*creating the committe transfer data that will be merged later
use committee.dta, clear
*Dataset Contributions to Candidates from Committees
*keeping only report that "covers through 20 days before the general election"
keep if  reporttype=="30G"
collapse (sum) amount, by(cand_id)
drop if missing(cand_id)
rename cand_id can_id
save transfer_collapsed2.dta, replace

*spending data comes from
*http://www.fec.gov/data/CandidateDisbursement.do?format=html&election_yr=2012

*creating initial dataset with Alaska, appending using other states
use ak.dta, clear

*dropping any memo entries, as they are repeats
drop if mem_cod == "X"
 
*keeping only the necessary variables
keep can_id ele_yea can_off can_off_sta can_off_dis ///
 dis_dat dis_amo dis_pur_des cat_cod cat_des  rec_sta
 
*office is not a string in all of the data
gen district = string(can_off_dis)
drop can_off_dis
rename district can_off_dis

save spending_all_states.dta, replace
sleep 1000


*appending all states for which cat_cod is a string
foreach file in "al" "ar" "az" "co" "ct" "de" "ga" ///
"hi" "ia" "id" "il" "ks" "ky" "la" "ma" "md" "me" "mn" ///
 "ms" "mt" "nc" "nd" "ne"  "nm" "nv" "ny"   {
use `file', clear
drop if mem_cod == "X"
keep can_id can_off can_off_sta can_off_dis com_id ///
 dis_dat dis_amo dis_pur_des cat_cod cat_des  rec_sta
gen district = string(can_off_dis)
drop can_off_dis
rename district can_off_dis
gen ele_yea=2012
append using spending_all_states.dta
compress
save spending_all_states.dta, replace
sleep 1000
}


*states that have strings for category codes have to be done seperately
foreach file in "ca" "fl" "mi" "mo"  "nj" {
use `file', clear
drop if mem_cod == "X"
keep can_id can_off can_off_sta can_off_dis com_id ///
 dis_dat dis_amo dis_pur_des cat_cod cat_des  rec_sta
gen ele_yea=2012
gen district = string(can_off_dis)
drop can_off_dis
rename district can_off_dis
gen category = real(cat_cod)
drop cat_cod
rename category cat_cod
compress
append using spending_all_states.dta
compress
save spending_all_states.dta, replace
sleep 1000
}


foreach file in   ///
     {
use `file', clear
drop if mem_cod == "X"
keep can_id can_off can_off_sta can_off_dis com_id ///
 dis_dat dis_amo dis_pur_des cat_cod cat_des  rec_sta
gen ele_yea=2012
gen district = string(can_off_dis)
drop can_off_dis
rename district can_off_dis
gen category = real(cat_cod)
drop cat_cod
rename category cat_cod
compress
append using spending_all_states.dta
save spending_all_states.dta, replace
sleep 1000
}


foreach file in   "nh" "oh" "ok" "or" "pa" "ri" "sc" "sd" "tn" "tx" "ut" "va" "vt" "wa" "wi" "wv" "wy" ///
   {
use `file', clear
drop if mem_cod == "X"
keep can_id can_off can_off_sta can_off_dis com_id ///
 dis_dat dis_amo dis_pur_des cat_cod cat_des  rec_sta
gen ele_yea=2012
gen category = real(cat_cod)
drop cat_cod
rename category cat_cod
compress
append using spending_all_states.dta
save spending_all_states.dta, replace
sleep 1000
}


use in.dta, clear
replace  cat_cod ="" if  cat_cod=="0" |  cat_cod=="SSS"
drop if mem_cod == "X"
keep can_id can_off can_off_sta can_off_dis com_id ///
 dis_dat dis_amo dis_pur_des cat_cod cat_des  rec_sta
gen ele_yea=2012
gen district = string(can_off_dis)
drop can_off_dis
rename district can_off_dis
gen category = real(cat_cod)
drop cat_cod
rename category cat_cod
compress
append using spending_all_states.dta
save spending_all_states.dta, replace
sleep 1000
*/

*creating clean money variable, string to numeric
gen amount = subinstr( dis_amo,"$","",.)
replace amount = subinstr( amount,",","",.)
gen dis_amo2 = real(amount)
drop dis_amo amount
rename dis_amo2 dis_amo

replace cat_cod=. if cat_cod==0
replace cat_cod= . if cat_cod>12 & cat_cod<200


*creating categories for all spending
*dropping if missing any info
drop if missing( dis_pur_des) & missing(cat_cod) & missing( cat_des)

*dropping Michelle Bachmann's Presidential committee spending (for some reason assigned to her Congressional ID)
drop if com_id=="C00497511"

/*Code used b FEC
001	 Administrative/Salary/Overhead Expenses (e.g. rent, staff salaries, postage, office supplies, equipment, furniture, ballot access fees, petition drives, party fees and legal and accounting expenses
002	 Travel Expenses  including travel reimbursement expenses (e.g., costs of commercial carrier tickets; reimbursements for use of private vehicles, advance payments for use of corporate aircraft; lodging and meal expenses incurred during travel)
003	 Solicitation and Fundraising Expenses (e.g., costs for direct mail solicitations and fundraising events including printing, mailing lists, consultant fees, call lists, invitations, catering costs and room rental)
004	 Advertising Expenses  including general public political advertising (e.g., purchases of radio/television broadcast/cable time, print advertisements and related production costs)
005	 Polling Expenses
006	 Campaign Materials (e.g., buttons, bumper stickers, brochures, mass mailings, pens, posters and balloons)
007	 Campaign Event Expenses (e.g., costs associated with candidate appearances, campaign rallies, town meetings, phone banks, including catering costs, door-to-door get-out-the-vote efforts and driving voters to the polls)
008	 Transfers (e.g., to other authorized committees of the same candidate)
009	 Loan Repayments (e.g., repayments of loans made or guaranteed by the candidate or any other person)
010	 Refunds of Contributions (e.g., contribution refunds to individuals/persons, political party committees or other political committees)
011	 Political Contributions (e.g., contributions to other federal candidates and committees, donations to non-federal candidates and committees)
012	 Donations (e.g., donations to charitable or civic organizations)*/

*creating cleaner code variable
rename cat_cod  code

*assigning codes for some of the most common descriptions
replace code = 1 if dis_pur_des=="ADMINISTRATIVE" & missing(code)
replace code = 1 if dis_pur_des=="SALARY" & missing(code)
replace code = 1 if dis_pur_des=="OVERHEAD" & missing(code)
replace code = 2 if dis_pur_des=="TRAVEL" & missing(code)
replace code = 3 if dis_pur_des=="FUNDRAISING" & missing(code)
replace code = 4 if dis_pur_des=="ADVERTISING" & missing(code)
replace code = 5 if dis_pur_des=="POLLING" & missing(code)
replace code = 5 if dis_pur_des=="POLLING EXPENSE" & missing(code)
replace code = 5 if dis_pur_des=="POLLING EXPENSES" & missing(code)
replace code = 6 if dis_pur_des=="CAMPAIGN MATERIALS" & missing(code)
replace code = 6 if dis_pur_des=="CAMPAIGN MATERIAL" & missing(code)
replace code = 6 if dis_pur_des=="MATERIALS" & missing(code)
replace code = 7 if dis_pur_des=="CAMPAIGN EVENT" & missing(code)
replace code = 7 if dis_pur_des=="CAMPAIGN EVENTS" & missing(code)
replace code = 7 if dis_pur_des=="CAMPAIGN EVENT EXPENSE" & missing(code)
replace code = 7 if dis_pur_des=="CAMPAIGN EVENT EXPENSES" & missing(code)
replace code = 8 if dis_pur_des=="TRANSFER" & missing(code)
replace code = 9 if dis_pur_des=="LOAN REPAYMENT" & missing(code)
replace code = 9 if dis_pur_des=="REPAYMENT OF LOAN" & missing(code)
replace code = 10 if dis_pur_des=="REFUND" & missing(code)
replace code = 10 if dis_pur_des=="REFUNDS" & missing(code)
replace code = 10 if dis_pur_des=="REFUND OF CONTRIBUTION" & missing(code)
replace code = 10 if dis_pur_des=="REFUND OF CONTRIBUTIONS" & missing(code)
replace code = 11 if dis_pur_des=="CONTRIBUTION" & missing(code)
replace code = 11 if dis_pur_des=="POLITICAL CONTRIBUTION" & missing(code)
replace code = 12 if dis_pur_des=="DONATION" & missing(code)
replace code = 12 if dis_pur_des=="DONATIONS" & missing(code)
replace code = 12 if dis_pur_des=="CHARITABLE DONATIONS" & missing(code)
replace code = 1 if dis_pur_des=="PAYROLL" & missing(code)
replace code = 1 if dis_pur_des=="PAYROLL TAXES" & missing(code)
replace code = 7 if dis_pur_des=="EVENT VENUE" & missing(code)
replace code = 7 if dis_pur_des=="EVENT CATERING" & missing(code)
replace code = 7 if dis_pur_des=="EVENT BALLROOM & BOARD" & missing(code)
replace code = 7 if dis_pur_des=="EVENT BASEBALL TICKETS" & missing(code)
replace code = 7 if dis_pur_des=="EVENT BEVERAGE" & missing(code)
replace code = 7 if dis_pur_des=="EVENT BEVERAGES" & missing(code)
replace code = 7 if dis_pur_des=="EVENT BEVERAGE SERVICE" & missing(code)
replace code = 3 if dis_pur_des=="CATERING" & missing(code)
replace code = 1 if dis_pur_des =="CC PROCESSING" & missing(code)
replace code = 1 if dis_pur_des =="CC PROCESSING CHARGE" & missing(code)
replace code = 1 if dis_pur_des =="CC PROCESSING FEE" & missing(code)
replace code = 4 if dis_pur_des =="ONLINE ADVERTISING" & missing(code)
replace code = 3 if dis_pur_des =="FOOD - FUNDRAISER"
replace code = 4 if dis_pur_des =="AD" & missing(code)
replace code = 4 if dis_pur_des =="ADVERTISING: DIRECT MAIL" & missing(code)
replace code = 4 if dis_pur_des =="ADVERTISING: EMAIL MARKETING" & missing(code)
replace code = 4 if dis_pur_des =="ADVERTISING: MAILER" & missing(code)
replace code = 4 if dis_pur_des =="ADVERTISING: LYNX POLO SHIRTS FOR GOLF TOURNAMENT"
replace code = 4 if dis_pur_des =="ADVERTISING: PRINTING" & missing(code)
replace code = 4 if dis_pur_des =="ADVERTISING: T-SHIRTS"
replace code = 4 if dis_pur_des =="ADVERTISING: TSHIRTS"
replace code = 4 if dis_pur_des =="ADVERTISMENT"
replace code = 1 if dis_pur_des=="SALARY & WAGES" & missing(code)
replace code = 1 if dis_pur_des=="SALARY EXPENSE" & missing(code)
replace code = 1 if dis_pur_des=="SOFTWARE" & missing(code)
replace code = 1 if dis_pur_des=="TELEPHONE" & missing(code)
replace code = 1 if dis_pur_des=="TELEPHONE CHARGES" & missing(code)
replace code = 1 if dis_pur_des=="TELEPHONE EXPENSE" & missing(code)
replace code = 1 if dis_pur_des=="TELEPHONE SERVICES" & missing(code)
replace code = 2 if dis_pur_des=="TRANSPORTATION" & missing(code)
replace code = 2 if dis_pur_des=="TOLLS" & missing(code)
replace code = 1 if dis_pur_des=="TONER" & missing(code)
replace code = 4 if dis_pur_des =="TELEVISION/RADIO ADS"
replace code = 4 if dis_pur_des =="TELEVISION/RADIO" & missing(code)
replace code = 4 if dis_pur_des =="TELEVISION AIRTIME"
replace code = 4 if dis_pur_des =="TELEVISION AIR TIME"
replace code = 4 if dis_pur_des =="TELEVISION ADVERTISING"
replace code = 4 if dis_pur_des =="TELEVISION ADS"
replace code = 4 if dis_pur_des =="TELEVISION ADS / MEDIA"
replace code = 4 if dis_pur_des =="TELEVISION & RADIO ADVERTISING"
replace code = 4 if dis_pur_des =="MEDIA-TV"
replace code = 4 if dis_pur_des =="MEDIA-RADIO"
replace code = 4 if dis_pur_des =="MEDIA-TELEVISION"
replace code = 4 if dis_pur_des =="MEDIA-TV EXPENSE"
replace code = 4 if dis_pur_des =="MEDIA/ADVERTISEMENT"
replace code = 4 if dis_pur_des =="MEDIA PRODUCTION SERVICES" & missing(code)
replace code = 10 if dis_pur_des =="CONTRIBUTION REFUND" & missing(code)
replace code = 11 if dis_pur_des =="POLITICAL CONTRIBUTION"
replace code = 1 if dis_pur_des=="CREDIT CARD PROCESSING FEE" & missing(code)
replace code = 1 if dis_pur_des=="OFFICE SUPPLIES" & missing(code)
replace code = 1 if dis_pur_des=="POSTAGE" & missing(code)
replace code = 1 if dis_pur_des=="CREDIT CARD PROCESSING FEES" & missing(code)
replace code = 1 if dis_pur_des=="TRANSACTION FEES" & missing(code)
replace code = 11 if dis_pur_des=="TRANSFER OF EXCESS FUNDS" & missing(code)
replace code = 1 if dis_pur_des=="SECURITY DEPOSIT" & missing(code)
replace code = 3 if dis_pur_des =="* IN-KIND - CATERING" & missing(code)
replace code = 3 if dis_pur_des =="* IN-KIND->CATERING" & missing(code)
replace code = 1 if dis_pur_des =="(FLAGS)" & missing(code)
replace code = 1 if dis_pur_des =="(IN KIND) RENT - 2 MONTHS" & missing(code)
replace code = 1 if dis_pur_des =="(CASH) DESKTUP - PC/PC SOFTWARE" & missing(code)
replace code = 1 if dis_pur_des =="#10 ENVELOPES" & missing(code)
replace code = 1 if dis_pur_des=="$68.90 FACILITY RENTAL CITY OF FOLLANSBEE, WV; I$44.93 INK CARTRIDGE, TARGET N FAYETTE, PA; FOOD & S"
replace code = 1 if dis_pur_des=="$7.41 PANERA BREAD, VIENNA, WV; $34.50 USPS, VIENNA, WV:  $159.56 OFFICE DEPOT, PARKERSBURG, WV"
replace code = 4 if dis_pur_des=="DOOR TO DOOR VOTER PERSUASION"
replace code = 1 if dis_pur_des=="CC BANK FEE"

/*assigning codes to entries that have some substring that will 
allow me to determine the purpose of the expenditure*/
generate splitat = strpos(dis_pur_des,"AIRFARE")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"GAS EXPENSE")
replace code = 2 if splitat>0 & missing(code)
drop splitat
gen splitat = strpos(dis_pur_des,"CREDIT CARD")
replace code = 1 if splitat>0 & missing(code)
drop splitat
gen splitat = strpos(dis_pur_des,"ADVERTISEMENT")
replace code = 4 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"LINE TICKET")
replace code = 2 if splitat>0 & missing(code)
drop splitat
gen splitat = strpos(dis_pur_des,"ADVERTISING")
replace code = 4 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRAVEL/")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRAVEL ")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SOFTWARE")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SALARY")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"RENT,")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"RENT-")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"MILEAGE")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FUNDRAISING")
replace code = 3 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FUEL ")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"F/R")
replace code = 3 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRANSPORTATION")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BANK FEE")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE SUPPLIES")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CONTRIBUTION ")
replace code = 11 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PAYROLL")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"-TRAVEL")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CAMPAIGN CONSULTANT")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"EVENTS")
replace code = 7 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"EVENT ")
replace code = 7 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"EMPLOYEE ")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"YARDSIGN")
replace code = 6 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"YARD SIGN")
replace code = 6 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WORKMAN")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WORKERS")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WIRELESS")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WEB SVC")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WEB DESIGN")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WAGES")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"VISA ")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"VENUE")
replace code = 7 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"VEHICLE")
replace code = 7 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"UTILITIES")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"UNLIMITED TRANSFER TO")
replace code = 8 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"MEDIA BUY")
replace code = 4 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SUPPLIES ")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"STAFF TIME")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SIGNS,")
replace code = 6 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SIGNS ")
replace code = 6 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SERVICE CHARGE")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"POSTAGE")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SALARIES")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"STAMPS")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PETTY CASH")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE EXPENSES")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"/GAS")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des," TRAVEL")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TELECOMMUNICATION")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"REFUND:")
replace code = 10 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"REFUND")
generate splitat1 = strpos(dis_pur_des,"CONTRIB")
replace code = 10 if splitat>0 & splitat1>0& missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"REFUND")
generate splitat1 = strpos(dis_pur_des,"DONAT")
replace code = 10 if splitat>0 & splitat1>0& missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"RECYCLING")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"EMARKET")
replace code = 4 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CATERING")
replace code = 7 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"DOOR-TO-DOOR")
replace code = 4 if splitat>0
drop splitat
generate splitat = strpos(dis_pur_des,"DOOR TO DOOR")
replace code = 4 if splitat>0
drop splitat
generate splitat = strpos(dis_pur_des,"DOOR-TO DOOR")
replace code = 4 if splitat>0
drop splitat
generate splitat = strpos(dis_pur_des,"DONATION")
replace code = 12 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE SPACE")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CAMPAIGN MATERIALS ")
replace code = 6 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADVERT")
replace code = 4 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PRINTING")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE EQUIPMENT")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE FURNITURE")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"UNEMPLOYMENT TAX")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HVAC")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HOUSING")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HOTEL ")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FUNDRAISER")
replace code = 3 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HEALTH INSURANCE")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HEALTH CARE")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HEALTH BENEFITS")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HEALTH & DENTAL")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HEALTH AND LIFE")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"XEROX")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE FACILITIES")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TV MEDIA")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TV BUY")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"UNEMPLOY TAX")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TV")
generate splitat1 = strpos(dis_pur_des,"AD")
replace code = 4 if splitat>0 & splitat1>0
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"RADIO")
generate splitat1 = strpos(dis_pur_des,"AD")
replace code = 4 if splitat>0 & splitat1>0
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"BUMPER STICKER")
replace code = 6 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TREASUR")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRAVL")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRVL")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRAVEL:")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRAVEL-")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRAVEL,")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRANSFER:")
replace code = 8 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRANSFER TO")
replace code = 8 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRANSFER OF")
replace code = 8 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRAIN TICKET")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TOLL ")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WORKER")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TELEPHONE SER")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TELEPHONE EXP")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TELEPHONE EQ")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SOCIAL MEDIA MARKETING")
replace code = 4 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SHIPPING ")
replace code = 1 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CAMPAIGN SUPPLIES")
replace code = 6 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"W/H TAX")
replace code = 1 if splitat>0 & missing(code)
drop splitat
replace code = 6 if dis_pur_des=="SCREEN PRINTING"
generate splitat = strpos(dis_pur_des,"RESTAURANT")
replace code = 2 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"REPAY ")
replace code = 9 if splitat>0 & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"REPAYMENT")
generate splitat1 = strpos(dis_pur_des,"LOAN")
replace code = 9 if splitat>0 & splitat1>0 &missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"CAR")
generate splitat1 = strpos(dis_pur_des,"FUEL")
replace code = 2 if splitat>0 & splitat1>0 &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"RENTAL CAR")
replace code = 2 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"RENT/")
replace code = 1 if splitat>0  & missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"RENT")
generate splitat1 = strpos(dis_pur_des,"OFFICE")
replace code = 1 if splitat>0 & splitat1>0 &missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"RENT -")
replace code = 1 if splitat>0  &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"RENT ")
replace code = 1 if splitat>0  &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ENVELOPES")
replace code = 1 if splitat>0  &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"GASOLINE")
replace code = 2 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AUTO EXP")
replace code = 2 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PARKING")
replace code = 2 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TELEPHONE SVC")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"LODGING")
replace code = 2 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"QUARTERLY TAX")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PROPERTY TAX")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"T-SHIRT")
replace code = 6 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"GAS CARD")
replace code = 2 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PO BOX")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PLACED MEDIA")
replace code = 4 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PHONE/")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PENCIL")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PAYMENT FOR AD")
replace code = 4 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PAYCHEX")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BILLBOARD")
replace code = 4 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des," STAFF")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"P.O. BOX")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"P. O. BOX")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"P.O BOX")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"COURIER")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"MAILER")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE WORK")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OTHER: TELEVISION")
replace code = 4 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SUPPLIES")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BLAST EMAIL")
replace code = 4 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"OUTREACH")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WIRE TRANSFER")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SERVICE FEE")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FILE STORAGE")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AD BUY")
replace code = 4 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ONLINE AD")
replace code = 4 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRANSACTION FEE")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE-")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE SUPPL")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE TEL")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE STORAGE")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"OFFICE ")
replace code = 1 if splitat==1   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MEDIA TIME")
replace code = 4 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MEDIA PURC")
replace code = 4 if splitat>0  &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MEDIA PAYM")
replace code = 4 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MEDIA COV")
replace code = 4 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MEDIA CONSUL")
replace code = 1 if splitat>0  &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MEDIA -")
replace code = 4 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MED INS")
replace code = 1 if splitat>0  &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"FOR EVENT")
replace code = 7 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MEAL ")
replace code = 1 if splitat==1   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MARKETING")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MANAGEMENT")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"MAINTENANCE")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"LUNCH WITH")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"LONG DISTANCE")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"LONG-DISTANCE")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"LOCAL TAX")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"LOAN RE")
replace code = 9 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"LOAN PAY")
replace code = 9 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"LEGAL FEE")
replace code = 1 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"LEGAL &")
replace code = 1 if splitat>0   &missing(code) 
replace code = 4 if dis_pur_des=="LABOR DAY AD"
drop splitat
generate splitat = strpos(dis_pur_des,"JOURNAL AD")
replace code = 4 if splitat>0   &missing(code) 
drop splitat
generate splitat = strpos(dis_pur_des,"JANITOR")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"IT SUPPORT")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"INTERNET/")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"INTERNET,")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"INTERNET EXP")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"WEB AD")
replace code = 4 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des," TOLLS ")
replace code = 2 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"- OFFICE")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des," MEAL ")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des," MEALS ")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FUND RAISING")
replace code = 3 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FOR RECEPTION")
replace code = 7 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des," AT EVENT")
replace code = 7 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des," CAMPAIGN EVENT")
replace code = 7 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HEALTHCARE")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HEADQUARTER")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HALL RENTAL")
replace code = 7 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"GROUND TRANSPORATION")
replace code = 2 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"GOLF")
replace code = 7 if splitat==1   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"GIFT")
replace code = 1 if splitat==1   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CONSULTING")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FOR VOLUNT")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PAIGN TRIP")
replace code = 2 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"GARBAGE")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FUNDRA")
replace code = 3 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FUND RA")
replace code = 3 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FR ")
replace code = 3 if splitat==1   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FOR SUPPORTERS")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CONSULTANT")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FILING FEE")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FEDERAL")
generate splitat1 = strpos(dis_pur_des,"TAX")
replace code = 1 if splitat>0  &splitat1>0 &missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"FED.CONT")
replace code = 11 if splitat>0   &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"FEC PREP")
replace code = 1 if splitat>0   &missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FEC COMP")
replace code = 1 if splitat>0   &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"EVENT:")
replace code = 7 if splitat>0   &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"EVENT/")
replace code = 7 if splitat>0   &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"AIGN SIGNS")
replace code = 6 if splitat>0   &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"SVCS")
replace code = 1 if splitat>0   &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"ECONOMIST")
replace code = 1 if splitat>0   &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"CAMPAIGN CONT")
replace code = 11 if splitat>0   &missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"DONATED ")
replace code = 12 if splitat>0   & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"FOR ")
generate splitat1 = strpos(dis_pur_des,"EVENT")
replace code = 7 if splitat>0  & splitat1>0 & missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"PROCESSING")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"DISCLOSURE")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"DATABASE")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SVC")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CUSTODIAL")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CONTRIB")
replace code = 11 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"COMPLIANCE")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"INSURANCE")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des," GOLF ")
replace code = 7 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CELLULAR PH")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CELLULAR SERV")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CELL PH")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CEL PH")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CC ")
replace code = 1 if splitat==1   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CAR")
generate splitat1 = strpos(dis_pur_des,"RENTAL")
replace code = 1 if splitat>0  &splitat1>0 & missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"FILING")
replace code = 1 if splitat>0   & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CAMPAIGN VISIB")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TEE SHIRT")
replace code = 6 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"T SHIRT")
replace code = 6 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TSHIRT")
replace code = 6 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CAMPAIGN STRA")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CAMPAIGN OFFI")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADVERSTISEMENT")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"LUNCH MEET")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"INTERNET AD")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADV ")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADV.")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CABLE/")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BROADBAND")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BOOKKEEPING")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BANQUET")
replace code = 7 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"DINNER EXPE")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BANK")
generate splitat1 = strpos(dis_pur_des,"FEE")
replace code = 1 if splitat>0  & splitat1>0  &  missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des," EVENT")
replace code = 7 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CILITY RENTAL")
replace code = 7 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FAXING")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PAGE AD")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PAPER AD")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"HQ")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ELECTION REFUND")
replace code = 11 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AIRLINE")
replace code = 2 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,":MEAL")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"941")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ACCOUNTING")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AD ")
replace code = 4 if splitat==1  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADMINISTRATIVE")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADS ")
replace code = 4 if splitat==1  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADVERSITING")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADVERSISING")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADVESTISING")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AIR FARE")
replace code = 2 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AIRCRAFT")
replace code = 2 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"002 MEALS")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AIRTIME")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ATM")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BANK")
replace code = 1 if splitat==1  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TAXES")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BIRTHDAY")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BLDG")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BUS ")
replace code = 2 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CABLE ")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SIGNAGE")
replace code = 6 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ADVISOR")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CAMPAIGN AD")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PAIGN CAR")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"EMAIL BLAST")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"PAIGN FOOD")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"NTERNET SERV")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"LEASE")
gen splitat1=strpos(dis_pur_des,"CAR")
replace code = 1 if splitat>0 & splitat>0 & missing(code)
drop splitat  splitat1
generate splitat = strpos(dis_pur_des,"MERCHANDIS")
replace code = 6 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"OFC")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"TEE-SHIR")
replace code = 6 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"HOSTING")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"DATE LOAN")
replace code = 9 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"CELLULAR")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"CHARITY")
replace code = 12 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"CHARTER")
replace code = 2 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WITHHOLDING")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des," CC ")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"CLERICAL")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"COFFEE")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"COMMERCIALS")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"COMMITTEE")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"COPIER")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEBSITE DE")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEBSITE AD")
replace code = 4 if splitat>0  & missing(code)
replace code = 1 if dis_pur_des =="WEBSITE"
drop splitat 
generate splitat = strpos(dis_pur_des,"WIRE FEE")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEBSITE HOS")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEBSITE EXP")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEB SERV")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEB HOS")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEB EXP")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"FOOD/BEV")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"FOOD & BEV")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"FOOD AND BEV")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"FOR SIGN")
replace code = 6 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"CC")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"TECH SUPP")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"DOMAIN")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEBHOST")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"WEB SITE")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"W/H")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"VOLUNTEER")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"VAN")
replace code = 2 if splitat==1  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"UTILI")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des," TAX ")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"UNLIMITED TRAN")
replace code = 8 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"TRANSFER ")
replace code = 8 if splitat==1  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"MEDIA AD")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"MEDIA/")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"MEDIA")
generate splitat1 = strpos(dis_pur_des,"BUY")
replace code = 4 if splitat>0 &  splitat1>0 & missing(code)
drop splitat  splitat1
generate splitat = strpos(dis_pur_des,"COMMERCIA")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AIR TIME")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"AVERTISING")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TRANSFER ")
replace code = 8 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"DIRECT MAIL")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ICAL MAILI")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"FEC REPO")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"VOTER MAI")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BALLOT")
generate splitat1 = strpos(dis_pur_des,"FEE")
replace code = 1 if splitat>0 &splitat1>0 & missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"BARN SI")
replace code = 6 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,":TRAVEL")
replace code = 2 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"GRAPHIC DES")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"BONUS")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"COUNSEL")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"MANAGERIAL")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"TAX ")
replace code = 1 if splitat==1  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"MEDIA")
replace code = 4 if splitat==1  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"PAID MEDIA")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"TRANSFER")
replace code = 8 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"DUES")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"SURVEY")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"EXCESS FUNDS")
replace code = 10 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"POLLING")
replace code = 5 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"LEGAL SER")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"VOTER CONTACT")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"DEBT PAY")
replace code = 9 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"PRODUCTION")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"ONLINE MEDIA")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"ANALYSIS")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"CALL SERVIC")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"MAIL SERVIC")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"BROADCAST MEDIA")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"TELEVISION BUY")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"RESEARCH")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"LIST RENT")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"TXFR")
replace code = 8 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"PHONE CALL")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"MAILINGS")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"STATIONARY")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"STORAGE")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"TRAINING")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"PROMOTIONAL")
replace code = 6 if splitat>0  & missing(code)
replace code = 1 if dis_pur_des=="RENT"
drop splitat  
generate splitat = strpos(dis_pur_des,"ARY RENT")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"BER RENT")
replace code = 1 if splitat>0  & missing(code)
replace code = 1 if dis_pur_des=="MARCH RENT"
replace code = 1 if dis_pur_des=="APRIL RENT"
replace code = 1 if dis_pur_des=="MAY RENT"
replace code = 1 if dis_pur_des=="JULY RENT"
replace code = 1 if dis_pur_des=="JUNE RENT"
replace code = 1 if dis_pur_des=="AUGUST RENT"
replace code = 10 if dis_pur_des=="REFUND"
drop splitat  
generate splitat = strpos(dis_pur_des," LABOR")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"COORDINATOR")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"DONOR REFUND")
replace code = 10 if splitat>0  & missing(code)
replace code =1 if dis_pur_des=="CANVASSING"
drop splitat  
generate splitat = strpos(dis_pur_des,"PHONE CHARGE")
replace code = 1 if splitat>0  & missing(code)
replace code =1 if dis_pur_des=="FOOD"
replace code =4 if dis_pur_des=="AD"
replace code =4 if dis_pur_des=="ADS"
replace code =4 if dis_pur_des=="TELEVISION"
replace code =4 if dis_pur_des=="PRINTED MEDIA"
drop splitat  
generate splitat = strpos(dis_pur_des,"RE-PAID PERSONAL LOAN")
replace code = 9 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"DIGITAL MEDIA")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"RETURN OF LOAN")
replace code = 9 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"DEBT REPAY")
replace code = 9 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"TV TIME")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"SFER OF FUN")
replace code = 8 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"MAIL ADS")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"FEC")
generate splitat1 = strpos(dis_pur_des,"REPORT")
replace code = 1 if splitat>0 & splitat1>0 & missing(code)
drop splitat   splitat1
generate splitat = strpos(dis_pur_des,"COMMERICAL")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des," POLL")
replace code = 5 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des," POLING")
replace code = 5 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"PROPERTY")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"BENEFITS")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"DIRECT DEP")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"COPIES")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"FIELD")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"TELEVISION AD")
replace code = 4 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"SEVERANCE")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"TOURNAMENT")
replace code = 7 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"CONVENTION")
replace code = 7 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des," PARTY")
replace code = 7 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"STAFF")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"POLL ")
replace code = 5 if splitat==1  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"DEVELOPMENT")
replace code = 3 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"STICKER")
replace code = 6 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"REPORTING")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"REFUND")
replace code = 10 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"ATTORNEY")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"PHOTOG")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"VIDEOG")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"DIRECTOR")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"FLOWER")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"HOTEL")
replace code = 2 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"PHONE BILL")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"LAON")
replace code = 9 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"MOBILE OFF")
replace code = 1 if splitat>0  & missing(code)
drop splitat  
generate splitat = strpos(dis_pur_des,"CONSULANT")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"TELECOM")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"TECHNOLOGY")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"TAXI")
replace code = 2 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"TABLE SPONSOR")
replace code = 12 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"STRATEGY")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"STRATEGIC")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"SPACE RENT")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"AUCTION ITEM")
replace code = 12 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"SIGN POST")
replace code = 12 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"SECURITY DEPOSIT")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"SECRETAR")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"SECURITY")
replace code = 1 if splitat==1  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"ROOM RENTAL")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"SUBSCRIPTION")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"MEETING EXP")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"CAMPAIGN CALL")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"MAILING SER")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"LITERATUR")
replace code = 6 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"LOAN INT")
replace code = 9 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"MAIL EXP")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"POLITICAL MAI")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"RE-PA")
replace code = 9 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"PAY")
generate splitat1 = strpos(dis_pur_des,"LOAN")
replace code = 9 if splitat>0  & splitat1>0 & missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"WEBSITE")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"DATA SERV")
replace code = 1 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"CANVASS")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"SOCIAL MEDIA")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"PARAPHANALIA")
replace code = 6 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"PARAPHERNALIA")
replace code = 6 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"EXCESS")
generate splitat1 = strpos(dis_pur_des,"FUND")
replace code = 10 if splitat>0  & splitat1>0 & missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"MOBILE PH")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"SUPPORTER GIFT")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"COMPUTER EQ")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"NEWSPRINT SPA")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"RACE CAR SPON")
replace code = 4 if splitat>0  & missing(code)
drop splitat 
generate splitat = strpos(dis_pur_des,"AUTOMATED")
generate splitat1 = strpos(dis_pur_des,"CALL")
replace code = 4 if splitat>0  & splitat1>0 & missing(code)
drop splitat splitat1
generate splitat = strpos(dis_pur_des,"ROBO")
generate splitat1 = strpos(dis_pur_des,"CALL")
replace code = 4 if splitat>0  & splitat1>0 & missing(code)
drop splitat splitat1
replace code = 4 if  dis_pur_des=="MAIL"
generate splitat = strpos(dis_pur_des,"CAMPAIGN MAIL")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"RINTED MATER")
replace code = 6 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"COMPUTER EX")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"BROCHURE")
replace code = 6 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"ICAN EXPRE")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CONCERT TOUR")
replace code = 2 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"DEBT FORGIVEN")
replace code = 9 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"TV SPOT")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"MAILING EXP")
replace code = 4 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"COPYING")
replace code = 1 if splitat>0  & missing(code)
replace code = 2 if  dis_pur_des=="FLIGHT"
drop splitat
generate splitat = strpos(dis_pur_des,"CONSUTLANT")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"CONSULT")
replace code = 1 if splitat>0  & missing(code)
drop splitat
generate splitat = strpos(dis_pur_des,"EQUIPMENT REN")
replace code = 1 if splitat>0  & missing(code)
drop splitat

egen pop = mode(code), by(dis_pur_des)
replace code = pop if missing(code)
drop pop 

save spending_all_states.dta, replace

*****************************************graph1*************************************
keep if can_off=="H"
collapse (sum) dis_amo, by(can_id code)
drop if missing(code)
reshape wide  dis_amo, i( can_id) j(code)
*merging with a candidate list that tells use Incembent/Challenger/Open Status
merge 1:1 can_id using cand_list.dta
foreach var of varlist dis_amo1- dis_amo12 {
replace `var' = 0 if missing(`var')
}

egen total_spend = rowtotal( dis_amo1- dis_amo12)
foreach var of varlist dis_amo1- dis_amo12 {
replace `var' = `var'*100/total_spend
}

collapse (mean)  dis_amo1 dis_amo2 dis_amo3 dis_amo4 /// 
dis_amo5 dis_amo6 dis_amo7 dis_amo8 dis_amo9 dis_amo10  ///
dis_amo11 dis_amo12, by(can_inc_cha_ope_sea)

reshape long  dis_amo, i( can_inc_cha_ope_sea) j(type)
drop if missing( can_inc_cha_ope_sea)
reshape wide dis_amo, j(type) i(can_inc)
xpose, clear varname
rename  v1 challenger
rename  v2 incumbent
rename  v3 open
replace _varname = "1" in 2
replace _varname = "2" in 3
replace _varname = "3" in 4
replace _varname = "4" in 5
replace _varname = "5" in 6
replace _varname = "6" in 7
replace _varname = "7" in 8
replace _varname = "8" in 9
replace _varname = "9" in 10
replace _varname = "10" in 11
replace _varname = "11" in 12
replace _varname = "12" in 13
drop in 1
gen type = real( _varname)
label define type2 1 "Administrative" 2 "Travel" 3 "Fundraising" 4 "Advertising" ///
5 "Polling" 6 "Materials" 7 "Events" 8 "Transfers" 9 "Loans Repayment" 10 "Refunds" ///
11 "Contributions" 12 "Donations"
label values  type type2
graph bar  challenger incumbent open, over(type, label(angle(60))) ///
 ytitle("Share of Total Spending") ///
 xsize(6) ///
 ylabel(0(10)42) ///
 legend(pos(2) ring(0) col(1)) ///
 title("Spending Patterns: Incumbent, Challengers, and Open Seats") ///
 legend( label(1 "Challenger") label(2 "Incumbent") label(3 "Open"))
 
 graph save Graph Graph_Codes.gph, replace
 
 *****************************************graph (close races)*************************************
use spending_all_states.dta, replace
egen ID = concat(can_off_sta can_off_dis) , punc(-)
keep if ID=="AZ-1"|ID=="AZ-2"|ID=="AZ-9"|ID=="CA-7"|ID=="CA-9"|ID=="CA-10"|ID=="CA-24"| ///
ID=="CA-26"|ID=="CA-36"|ID=="CA-41"|ID=="CA-52"|ID=="CO-3"|ID=="CO-6"|ID=="CO-7"|ID=="CT-5"| ///
ID=="FL-2"|ID=="FL-10"|ID=="FL-18"|ID=="FL-26"|ID=="GA-12"|ID=="IL-10"|ID=="IL-11"|ID=="IL-12"| ///
ID=="IL-13"|ID=="IL-17"|ID=="IN-8"|ID=="IA-3"|ID=="IA-4"|ID=="KY-6"|ID=="MA-6"|ID=="MI-1"| ///
ID=="MI-11"|ID=="MN-8"|ID=="NV-4"|ID=="NH-1"|ID=="NH-2"|ID=="NJ-3"|ID=="NY-1"|ID=="NY-18"| ///
ID=="NY-19"|ID=="NY-21"|ID=="NY-24"|ID=="NY-27"|ID=="NC-7"|ID=="OH-6"|ID=="OH-16"|ID=="PA-8"| ///
ID=="PA-12"|ID=="RI-1"|ID=="TN-4"|ID=="TX-14"|ID=="TX-23"|ID=="UT-4"|ID=="WI-7"

 keep if can_off=="H"
collapse (sum) dis_amo, by(can_id code)
drop if missing(code)
reshape wide  dis_amo, i( can_id) j(code)
*merging with a candidate list that tells use Incembent/Challenger/Open Status
merge 1:1 can_id using cand_list.dta
foreach var of varlist dis_amo1- dis_amo12 {
replace `var' = 0 if missing(`var')
}

egen total_spend = rowtotal( dis_amo1- dis_amo12)
foreach var of varlist dis_amo1- dis_amo12 {
replace `var' = `var'*100/total_spend
}

collapse (mean)  dis_amo1 dis_amo2 dis_amo3 dis_amo4 /// 
dis_amo5 dis_amo6 dis_amo7 dis_amo8 dis_amo9 dis_amo10  ///
dis_amo11 dis_amo12, by(can_inc_cha_ope_sea)

reshape long  dis_amo, i( can_inc_cha_ope_sea) j(type)
drop if missing( can_inc_cha_ope_sea)
reshape wide dis_amo, j(type) i(can_inc)
xpose, clear varname
rename  v1 challenger
rename  v2 incumbent
rename  v3 open
replace _varname = "1" in 2
replace _varname = "2" in 3
replace _varname = "3" in 4
replace _varname = "4" in 5
replace _varname = "5" in 6
replace _varname = "6" in 7
replace _varname = "7" in 8
replace _varname = "8" in 9
replace _varname = "9" in 10
replace _varname = "10" in 11
replace _varname = "11" in 12
replace _varname = "12" in 13
drop in 1
gen type = real( _varname)
label define type2 1 "Administrative" 2 "Travel" 3 "Fundraising" 4 "Advertising" ///
5 "Polling" 6 "Materials" 7 "Events" 8 "Transfers" 9 "Loans Repayment" 10 "Refunds" ///
11 "Contributions" 12 "Donations"
label values  type type2
graph bar  challenger incumbent open, over(type, label(angle(60))) ///
 ytitle("Share of Total Spending") ///
 xsize(6) ///
 ylabel(0(10)42) ///
 legend(pos(2) ring(0) col(1)) ///
 title("Spending Patterns: Incumbent, Challengers, and Open Seats") ///
 legend( label(1 "Challenger") label(2 "Incumbent") label(3 "Open"))
 
  graph save Graph Graph_Close.gph, replace

  
*******************PREPARING DATA TO MERGE*********************
use spending_all_states.dta, replace

*creating monotonic day code
gen year = substr( dis_dat, 1, 4)
keep if year =="2010"|year=="2011"|year=="2012"|year=="2013"
gen day =.
replace day = 366 if dis_dat== "2012-01-01"
replace day = 367 if dis_dat== "2012-01-02"
replace day = 368 if dis_dat== "2012-01-03"
replace day = 369 if dis_dat== "2012-01-04"
replace day = 370 if dis_dat== "2012-01-05"
replace day = 371 if dis_dat== "2012-01-06"
replace day = 372 if dis_dat== "2012-01-07"
replace day = 373 if dis_dat== "2012-01-08"
replace day = 374 if dis_dat== "2012-01-09"
replace day = 375 if dis_dat== "2012-01-10"
replace day = 376 if dis_dat== "2012-01-11"
replace day = 377 if dis_dat== "2012-01-12"
replace day = 378 if dis_dat== "2012-01-13"
replace day = 379 if dis_dat== "2012-01-14"
replace day = 380 if dis_dat== "2012-01-15"
replace day = 381 if dis_dat== "2012-01-16"
replace day = 382 if dis_dat== "2012-01-17"
replace day = 383 if dis_dat== "2012-01-18"
replace day = 384 if dis_dat== "2012-01-19"
replace day = 385 if dis_dat== "2012-01-20"
replace day = 386 if dis_dat== "2012-01-21"
replace day = 387 if dis_dat== "2012-01-22"
replace day = 388 if dis_dat== "2012-01-23"
replace day = 389 if dis_dat== "2012-01-24"
replace day = 390 if dis_dat== "2012-01-25"
replace day = 391 if dis_dat== "2012-01-26"
replace day = 392 if dis_dat== "2012-01-27"
replace day = 393 if dis_dat== "2012-01-28"
replace day = 394 if dis_dat== "2012-01-29"
replace day = 395 if dis_dat== "2012-01-30"
replace day = 396 if dis_dat== "2012-01-31"
replace day = 397 if dis_dat== "2012-02-01"
replace day = 398 if dis_dat== "2012-02-02"
replace day = 399 if dis_dat== "2012-02-03"
replace day = 400 if dis_dat== "2012-02-04"
replace day = 401 if dis_dat== "2012-02-05"
replace day = 402 if dis_dat== "2012-02-06"
replace day = 403 if dis_dat== "2012-02-07"
replace day = 404 if dis_dat== "2012-02-08"
replace day = 405 if dis_dat== "2012-02-09"
replace day = 406 if dis_dat== "2012-02-10"
replace day = 407 if dis_dat== "2012-02-11"
replace day = 408 if dis_dat== "2012-02-12"
replace day = 409 if dis_dat== "2012-02-13"
replace day = 410 if dis_dat== "2012-02-14"
replace day = 411 if dis_dat== "2012-02-15"
replace day = 412 if dis_dat== "2012-02-16"
replace day = 413 if dis_dat== "2012-02-17"
replace day = 414 if dis_dat== "2012-02-18"
replace day = 415 if dis_dat== "2012-02-19"
replace day = 416 if dis_dat== "2012-02-20"
replace day = 417 if dis_dat== "2012-02-21"
replace day = 418 if dis_dat== "2012-02-22"
replace day = 419 if dis_dat== "2012-02-23"
replace day = 420 if dis_dat== "2012-02-24"
replace day = 421 if dis_dat== "2012-02-25"
replace day = 422 if dis_dat== "2012-02-26"
replace day = 423 if dis_dat== "2012-02-27"
replace day = 424 if dis_dat== "2012-02-28"
replace day = 425 if dis_dat== "2012-02-29"
replace day = 426 if dis_dat== "2012-03-01"
replace day = 427 if dis_dat== "2012-03-02"
replace day = 428 if dis_dat== "2012-03-03"
replace day = 429 if dis_dat== "2012-03-04"
replace day = 430 if dis_dat== "2012-03-05"
replace day = 431 if dis_dat== "2012-03-06"
replace day = 432 if dis_dat== "2012-03-07"
replace day = 433 if dis_dat== "2012-03-08"
replace day = 434 if dis_dat== "2012-03-09"
replace day = 435 if dis_dat== "2012-03-10"
replace day = 436 if dis_dat== "2012-03-11"
replace day = 437 if dis_dat== "2012-03-12"
replace day = 438 if dis_dat== "2012-03-13"
replace day = 439 if dis_dat== "2012-03-14"
replace day = 440 if dis_dat== "2012-03-15"
replace day = 441 if dis_dat== "2012-03-16"
replace day = 442 if dis_dat== "2012-03-17"
replace day = 443 if dis_dat== "2012-03-18"
replace day = 444 if dis_dat== "2012-03-19"
replace day = 445 if dis_dat== "2012-03-20"
replace day = 446 if dis_dat== "2012-03-21"
replace day = 447 if dis_dat== "2012-03-22"
replace day = 448 if dis_dat== "2012-03-23"
replace day = 449 if dis_dat== "2012-03-24"
replace day = 450 if dis_dat== "2012-03-25"
replace day = 451 if dis_dat== "2012-03-26"
replace day = 452 if dis_dat== "2012-03-27"
replace day = 453 if dis_dat== "2012-03-28"
replace day = 454 if dis_dat== "2012-03-29"
replace day = 455 if dis_dat== "2012-03-30"
replace day = 456 if dis_dat== "2012-03-31"
replace day = 457 if dis_dat== "2012-04-01"
replace day = 458 if dis_dat== "2012-04-02"
replace day = 459 if dis_dat== "2012-04-03"
replace day = 460 if dis_dat== "2012-04-04"
replace day = 461 if dis_dat== "2012-04-05"
replace day = 462 if dis_dat== "2012-04-06"
replace day = 463 if dis_dat== "2012-04-07"
replace day = 464 if dis_dat== "2012-04-08"
replace day = 465 if dis_dat== "2012-04-09"
replace day = 466 if dis_dat== "2012-04-10"
replace day = 467 if dis_dat== "2012-04-11"
replace day = 468 if dis_dat== "2012-04-12"
replace day = 469 if dis_dat== "2012-04-13"
replace day = 470 if dis_dat== "2012-04-14"
replace day = 471 if dis_dat== "2012-04-15"
replace day = 472 if dis_dat== "2012-04-16"
replace day = 473 if dis_dat== "2012-04-17"
replace day = 474 if dis_dat== "2012-04-18"
replace day = 475 if dis_dat== "2012-04-19"
replace day = 476 if dis_dat== "2012-04-20"
replace day = 477 if dis_dat== "2012-04-21"
replace day = 478 if dis_dat== "2012-04-22"
replace day = 479 if dis_dat== "2012-04-23"
replace day = 480 if dis_dat== "2012-04-24"
replace day = 481 if dis_dat== "2012-04-25"
replace day = 482 if dis_dat== "2012-04-26"
replace day = 483 if dis_dat== "2012-04-27"
replace day = 484 if dis_dat== "2012-04-28"
replace day = 485 if dis_dat== "2012-04-29"
replace day = 486 if dis_dat== "2012-04-30"
replace day = 487 if dis_dat== "2012-05-01"
replace day = 488 if dis_dat== "2012-05-02"
replace day = 489 if dis_dat== "2012-05-03"
replace day = 490 if dis_dat== "2012-05-04"
replace day = 491 if dis_dat== "2012-05-05"
replace day = 492 if dis_dat== "2012-05-06"
replace day = 493 if dis_dat== "2012-05-07"
replace day = 494 if dis_dat== "2012-05-08"
replace day = 495 if dis_dat== "2012-05-09"
replace day = 496 if dis_dat== "2012-05-10"
replace day = 497 if dis_dat== "2012-05-11"
replace day = 498 if dis_dat== "2012-05-12"
replace day = 499 if dis_dat== "2012-05-13"
replace day = 500 if dis_dat== "2012-05-14"
replace day = 501 if dis_dat== "2012-05-15"
replace day = 502 if dis_dat== "2012-05-16"
replace day = 503 if dis_dat== "2012-05-17"
replace day = 504 if dis_dat== "2012-05-18"
replace day = 505 if dis_dat== "2012-05-19"
replace day = 506 if dis_dat== "2012-05-20"
replace day = 507 if dis_dat== "2012-05-21"
replace day = 508 if dis_dat== "2012-05-22"
replace day = 509 if dis_dat== "2012-05-23"
replace day = 510 if dis_dat== "2012-05-24"
replace day = 511 if dis_dat== "2012-05-25"
replace day = 512 if dis_dat== "2012-05-26"
replace day = 513 if dis_dat== "2012-05-27"
replace day = 514 if dis_dat== "2012-05-28"
replace day = 515 if dis_dat== "2012-05-29"
replace day = 516 if dis_dat== "2012-05-30"
replace day = 517 if dis_dat== "2012-05-31"
replace day = 518 if dis_dat== "2012-06-01"
replace day = 519 if dis_dat== "2012-06-02"
replace day = 520 if dis_dat== "2012-06-03"
replace day = 521 if dis_dat== "2012-06-04"
replace day = 522 if dis_dat== "2012-06-05"
replace day = 523 if dis_dat== "2012-06-06"
replace day = 524 if dis_dat== "2012-06-07"
replace day = 525 if dis_dat== "2012-06-08"
replace day = 526 if dis_dat== "2012-06-09"
replace day = 527 if dis_dat== "2012-06-10"
replace day = 528 if dis_dat== "2012-06-11"
replace day = 529 if dis_dat== "2012-06-12"
replace day = 530 if dis_dat== "2012-06-13"
replace day = 531 if dis_dat== "2012-06-14"
replace day = 532 if dis_dat== "2012-06-15"
replace day = 533 if dis_dat== "2012-06-16"
replace day = 534 if dis_dat== "2012-06-17"
replace day = 535 if dis_dat== "2012-06-18"
replace day = 536 if dis_dat== "2012-06-19"
replace day = 537 if dis_dat== "2012-06-20"
replace day = 538 if dis_dat== "2012-06-21"
replace day = 539 if dis_dat== "2012-06-22"
replace day = 540 if dis_dat== "2012-06-23"
replace day = 541 if dis_dat== "2012-06-24"
replace day = 542 if dis_dat== "2012-06-25"
replace day = 543 if dis_dat== "2012-06-26"
replace day = 544 if dis_dat== "2012-06-27"
replace day = 545 if dis_dat== "2012-06-28"
replace day = 546 if dis_dat== "2012-06-29"
replace day = 547 if dis_dat== "2012-06-30"
replace day = 548 if dis_dat== "2012-07-01"
replace day = 549 if dis_dat== "2012-07-02"
replace day = 550 if dis_dat== "2012-07-03"
replace day = 551 if dis_dat== "2012-07-04"
replace day = 552 if dis_dat== "2012-07-05"
replace day = 553 if dis_dat== "2012-07-06"
replace day = 554 if dis_dat== "2012-07-07"
replace day = 555 if dis_dat== "2012-07-08"
replace day = 556 if dis_dat== "2012-07-09"
replace day = 557 if dis_dat== "2012-07-10"
replace day = 558 if dis_dat== "2012-07-11"
replace day = 559 if dis_dat== "2012-07-12"
replace day = 560 if dis_dat== "2012-07-13"
replace day = 561 if dis_dat== "2012-07-14"
replace day = 562 if dis_dat== "2012-07-15"
replace day = 563 if dis_dat== "2012-07-16"
replace day = 564 if dis_dat== "2012-07-17"
replace day = 565 if dis_dat== "2012-07-18"
replace day = 566 if dis_dat== "2012-07-19"
replace day = 567 if dis_dat== "2012-07-20"
replace day = 568 if dis_dat== "2012-07-21"
replace day = 569 if dis_dat== "2012-07-22"
replace day = 570 if dis_dat== "2012-07-23"
replace day = 571 if dis_dat== "2012-07-24"
replace day = 572 if dis_dat== "2012-07-25"
replace day = 573 if dis_dat== "2012-07-26"
replace day = 574 if dis_dat== "2012-07-27"
replace day = 575 if dis_dat== "2012-07-28"
replace day = 576 if dis_dat== "2012-07-29"
replace day = 577 if dis_dat== "2012-07-30"
replace day = 578 if dis_dat== "2012-07-31"
replace day = 579 if dis_dat== "2012-08-01"
replace day = 580 if dis_dat== "2012-08-02"
replace day = 581 if dis_dat== "2012-08-03"
replace day = 582 if dis_dat== "2012-08-04"
replace day = 583 if dis_dat== "2012-08-05"
replace day = 584 if dis_dat== "2012-08-06"
replace day = 585 if dis_dat== "2012-08-07"
replace day = 586 if dis_dat== "2012-08-08"
replace day = 587 if dis_dat== "2012-08-09"
replace day = 588 if dis_dat== "2012-08-10"
replace day = 589 if dis_dat== "2012-08-11"
replace day = 590 if dis_dat== "2012-08-12"
replace day = 591 if dis_dat== "2012-08-13"
replace day = 592 if dis_dat== "2012-08-14"
replace day = 593 if dis_dat== "2012-08-15"
replace day = 594 if dis_dat== "2012-08-16"
replace day = 595 if dis_dat== "2012-08-17"
replace day = 596 if dis_dat== "2012-08-18"
replace day = 597 if dis_dat== "2012-08-19"
replace day = 598 if dis_dat== "2012-08-20"
replace day = 599 if dis_dat== "2012-08-21"
replace day = 600 if dis_dat== "2012-08-22"
replace day = 601 if dis_dat== "2012-08-23"
replace day = 602 if dis_dat== "2012-08-24"
replace day = 603 if dis_dat== "2012-08-25"
replace day = 604 if dis_dat== "2012-08-26"
replace day = 605 if dis_dat== "2012-08-27"
replace day = 606 if dis_dat== "2012-08-28"
replace day = 607 if dis_dat== "2012-08-29"
replace day = 608 if dis_dat== "2012-08-30"
replace day = 609 if dis_dat== "2012-08-31"
replace day = 610 if dis_dat== "2012-09-01"
replace day = 611 if dis_dat== "2012-09-02"
replace day = 612 if dis_dat== "2012-09-03"
replace day = 613 if dis_dat== "2012-09-04"
replace day = 614 if dis_dat== "2012-09-05"
replace day = 615 if dis_dat== "2012-09-06"
replace day = 616 if dis_dat== "2012-09-07"
replace day = 617 if dis_dat== "2012-09-08"
replace day = 618 if dis_dat== "2012-09-09"
replace day = 619 if dis_dat== "2012-09-10"
replace day = 620 if dis_dat== "2012-09-11"
replace day = 621 if dis_dat== "2012-09-12"
replace day = 622 if dis_dat== "2012-09-13"
replace day = 623 if dis_dat== "2012-09-14"
replace day = 624 if dis_dat== "2012-09-15"
replace day = 625 if dis_dat== "2012-09-16"
replace day = 626 if dis_dat== "2012-09-17"
replace day = 627 if dis_dat== "2012-09-18"
replace day = 628 if dis_dat== "2012-09-19"
replace day = 629 if dis_dat== "2012-09-20"
replace day = 630 if dis_dat== "2012-09-21"
replace day = 631 if dis_dat== "2012-09-22"
replace day = 632 if dis_dat== "2012-09-23"
replace day = 633 if dis_dat== "2012-09-24"
replace day = 634 if dis_dat== "2012-09-25"
replace day = 635 if dis_dat== "2012-09-26"
replace day = 636 if dis_dat== "2012-09-27"
replace day = 637 if dis_dat== "2012-09-28"
replace day = 638 if dis_dat== "2012-09-29"
replace day = 639 if dis_dat== "2012-09-30"
replace day = 640 if dis_dat== "2012-10-01"
replace day = 641 if dis_dat== "2012-10-02"
replace day = 642 if dis_dat== "2012-10-03"
replace day = 643 if dis_dat== "2012-10-04"
replace day = 644 if dis_dat== "2012-10-05"
replace day = 645 if dis_dat== "2012-10-06"
replace day = 646 if dis_dat== "2012-10-07"
replace day = 647 if dis_dat== "2012-10-08"
replace day = 648 if dis_dat== "2012-10-09"
replace day = 649 if dis_dat== "2012-10-10"
replace day = 650 if dis_dat== "2012-10-11"
replace day = 651 if dis_dat== "2012-10-12"
replace day = 652 if dis_dat== "2012-10-13"
replace day = 653 if dis_dat== "2012-10-14"
replace day = 654 if dis_dat== "2012-10-15"
replace day = 655 if dis_dat== "2012-10-16"
replace day = 656 if dis_dat== "2012-10-17"
replace day = 657 if dis_dat== "2012-10-18"
replace day = 658 if dis_dat== "2012-10-19"
replace day = 659 if dis_dat== "2012-10-20"
replace day = 660 if dis_dat== "2012-10-21"
replace day = 661 if dis_dat== "2012-10-22"
replace day = 662 if dis_dat== "2012-10-23"
replace day = 663 if dis_dat== "2012-10-24"
replace day = 664 if dis_dat== "2012-10-25"
replace day = 665 if dis_dat== "2012-10-26"
replace day = 666 if dis_dat== "2012-10-27"
replace day = 667 if dis_dat== "2012-10-28"
replace day = 668 if dis_dat== "2012-10-29"
replace day = 669 if dis_dat== "2012-10-30"
replace day = 670 if dis_dat== "2012-10-31"
replace day = 671 if dis_dat== "2012-11-01"
replace day = 672 if dis_dat== "2012-11-02"
replace day = 673 if dis_dat== "2012-11-03"
replace day = 674 if dis_dat== "2012-11-04"
replace day = 675 if dis_dat== "2012-11-05"
replace day = 676 if dis_dat== "2012-11-06"
replace day = 677 if dis_dat== "2012-11-07"
replace day = 678 if dis_dat== "2012-11-08"
replace day = 679 if dis_dat== "2012-11-09"
replace day = 680 if dis_dat== "2012-11-10"
replace day = 681 if dis_dat== "2012-11-11"
replace day = 682 if dis_dat== "2012-11-12"
replace day = 683 if dis_dat== "2012-11-13"
replace day = 684 if dis_dat== "2012-11-14"
replace day = 685 if dis_dat== "2012-11-15"
replace day = 686 if dis_dat== "2012-11-16"
replace day = 687 if dis_dat== "2012-11-17"
replace day = 688 if dis_dat== "2012-11-18"
replace day = 689 if dis_dat== "2012-11-19"
replace day = 690 if dis_dat== "2012-11-20"
replace day = 691 if dis_dat== "2012-11-21"
replace day = 692 if dis_dat== "2012-11-22"
replace day = 693 if dis_dat== "2012-11-23"
replace day = 694 if dis_dat== "2012-11-24"
replace day = 695 if dis_dat== "2012-11-25"
replace day = 696 if dis_dat== "2012-11-26"
replace day = 697 if dis_dat== "2012-11-27"
replace day = 698 if dis_dat== "2012-11-28"
replace day = 699 if dis_dat== "2012-11-29"
replace day = 700 if dis_dat== "2012-11-30"
replace day = 701 if dis_dat== "2012-12-01"
replace day = 702 if dis_dat== "2012-12-02"
replace day = 703 if dis_dat== "2012-12-03"
replace day = 704 if dis_dat== "2012-12-04"
replace day = 705 if dis_dat== "2012-12-05"
replace day = 706 if dis_dat== "2012-12-06"
replace day = 707 if dis_dat== "2012-12-07"
replace day = 708 if dis_dat== "2012-12-08"
replace day = 709 if dis_dat== "2012-12-09"
replace day = 710 if dis_dat== "2012-12-10"
replace day = 711 if dis_dat== "2012-12-11"
replace day = 712 if dis_dat== "2012-12-12"
replace day = 713 if dis_dat== "2012-12-13"
replace day = 714 if dis_dat== "2012-12-14"
replace day = 715 if dis_dat== "2012-12-15"
replace day = 716 if dis_dat== "2012-12-16"
replace day = 717 if dis_dat== "2012-12-17"
replace day = 718 if dis_dat== "2012-12-18"
replace day = 719 if dis_dat== "2012-12-19"
replace day = 720 if dis_dat== "2012-12-20"
replace day = 721 if dis_dat== "2012-12-21"
replace day = 722 if dis_dat== "2012-12-22"
replace day = 723 if dis_dat== "2012-12-23"
replace day = 724 if dis_dat== "2012-12-24"
replace day = 725 if dis_dat== "2012-12-25"
replace day = 726 if dis_dat== "2012-12-26"
replace day = 727 if dis_dat== "2012-12-27"
replace day = 728 if dis_dat== "2012-12-28"
replace day = 729 if dis_dat== "2012-12-29"
replace day = 730 if dis_dat== "2012-12-30"
replace day = 731 if dis_dat== "2012-12-31"
replace day = day-365

replace day = 0 if year=="2011"|year=="2010"
replace day = 367 if year=="2013"
save spending_all_states.dta, replace
 *****************************************ALL SPENDING*************************************
 collapse (sum)  dis_amo, by(can_id can_off can_off_sta can_off_dis day)
 gen cum_spend = .
 compress

foreach day of numlist 0/150 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend0- cum_spend150 {
replace cum_spend = `var' if cum_spend==.
}
drop cum_spend0- cum_spend150

foreach day of numlist 151/300 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend151- cum_spend300 {
replace cum_spend = `var' if cum_spend==.
}

drop cum_spend151- cum_spend300

foreach day of numlist 301/367 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend301- cum_spend367 {
replace cum_spend = `var' if cum_spend==.
}

drop cum_spend301- cum_spend367
*i need to fill in the missing day with the cumulative spending of the previous day
drop if missing(day)
drop dis_amo

reshape wide cum_spend, i(can_id can_off can_off_sta can_off_dis) j(day)
reshape long cum_spend, i(can_id can_off can_off_sta can_off_dis) j(day)
sort can_id day

/*replacing the missing value (on a day in which 0 was spent) with the closest 
previous cumulative spending value*/
by can_id: replace cum_spend = cum_spend[_n-1] if cum_spend==.
replace cum_spend= 0 if missing(cum_spend)

*detemining the beginning of the general election by the end of the primary
replace day = day+365
gen begin_day = .
replace begin_day = 1 if day==481 & can_off_sta=="AL"
replace begin_day = 1 if day==638 & can_off_sta=="AK"
replace begin_day = 1 if day==638 & can_off_sta=="AZ"
replace begin_day = 1 if day == 530 & can_off_sta=="AR"
replace begin_day = 1 if day == 523 & can_off_sta=="CA"
replace begin_day = 1 if day == 544 & can_off_sta=="CO"
replace begin_day = 1 if day == 603 & can_off_sta=="CT"
replace begin_day = 1 if day == 621 & can_off_sta=="DE"
replace begin_day = 1 if day == 595 & can_off_sta=="FL"
replace begin_day = 1 if day == 600 & can_off_sta=="GA"
replace begin_day = 1 if day == 590 & can_off_sta=="HI"
replace begin_day = 1 if day == 502 & can_off_sta=="ID"
replace begin_day = 1 if day == 446 & can_off_sta=="IL"
replace begin_day = 1 if day == 495 & can_off_sta=="IN"
replace begin_day = 1 if day == 523 & can_off_sta=="IA"
replace begin_day = 1 if day == 586 & can_off_sta=="KS"
replace begin_day = 1 if day == 530 & can_off_sta=="KY"
replace begin_day = 1 if day == 360 & can_off_sta=="LA"
replace begin_day = 1 if day == 530 & can_off_sta=="ME"
replace begin_day = 1 if day == 460 & can_off_sta=="MD"
replace begin_day = 1 if day == 616 & can_off_sta=="MA"
replace begin_day = 1 if day == 587 & can_off_sta=="MI"
replace begin_day = 1 if day == 593 & can_off_sta=="MN"
replace begin_day = 1 if day == 460 & can_off_sta=="MS"
replace begin_day = 1 if day == 586 & can_off_sta=="MO"
replace begin_day = 1 if day == 523 & can_off_sta=="MT"
replace begin_day = 1 if day == 502 & can_off_sta=="NE"
replace begin_day = 1 if day == 530 & can_off_sta=="NV"
replace begin_day = 1 if day == 621 & can_off_sta=="NH"
replace begin_day = 1 if day == 523 & can_off_sta=="NJ"
replace begin_day = 1 if day == 523 & can_off_sta=="NM"
replace begin_day = 1 if day == 544 & can_off_sta=="NY"
replace begin_day = 1 if day == 565 & can_off_sta=="NC"
replace begin_day = 1 if day == 530 & can_off_sta=="ND"
replace begin_day = 1 if day == 432 & can_off_sta=="OH"
replace begin_day = 1 if day == 607 & can_off_sta=="OK"
replace begin_day = 1 if day == 502 & can_off_sta=="OR"
replace begin_day = 1 if day == 481 & can_off_sta=="PA"
replace begin_day = 1 if day == 621 & can_off_sta=="RI"
replace begin_day = 1 if day == 544 & can_off_sta=="SC"
replace begin_day = 1 if day == 544 & can_off_sta=="SD"
replace begin_day = 1 if day == 591 & can_off_sta=="TN"
replace begin_day = 1 if day == 579 & can_off_sta=="TX"
replace begin_day = 1 if day == 544 & can_off_sta=="UT"
replace begin_day = 1 if day == 606 & can_off_sta=="VT"
replace begin_day = 1 if day == 530 & can_off_sta=="VA"
replace begin_day = 1 if day == 586 & can_off_sta=="WA"
replace begin_day = 1 if day == 495 & can_off_sta=="WV"
replace begin_day = 1 if day == 593 & can_off_sta=="WI"
replace begin_day = 1 if day == 600 & can_off_sta=="WY"

replace begin_day = 2 if day==676
replace begin_day = 3 if day==732


rename can_off_dis can_off_dis_code
*master FEC candidate data list taken from http://www.fec.gov/finance/disclosure/ftpdet.shtml#a2011_2012
merge m:1 can_id using cand_master_list.dta
keep if _merge ==1 | _merge==3
drop _merge

*merge with dataset that will identify whether the politician was a candidate in the general election
merge m:1 can_id using cand_list2.dta
*Some candidates are missing. Fixed below
replace candidate = 0 if can_id=="H2CA21159"
replace candidate = 0 if can_id=="H2MI01118"
replace candidate = 0 if can_id=="H2NV02239"
replace candidate = 0 if can_id=="H4IN02051"
replace candidate = 0 if can_id=="H6OH23033"

drop if missing( can_off_sta)

keep if candidate==1

*fixing mis-assigned candidates
replace can_party = "REP" if can_id=="H2NY05084"
replace can_party = "REP" if can_id=="H0GA07133"
replace can_party = "DEM" if can_id=="H2NY22113"
replace can_party = "REP" if can_id=="H2NY26171"
replace can_party = "REP" if can_id=="H2PA14084"
replace can_inc_cha_ope_sea="CHALLENGER" if can_id=="H2NY05084"
*generate party code (1=dem, 2=rep)
gen party = 1 if can_party=="DEM" | can_party=="DFL"
replace party = 2 if can_party=="REP"
keep if party== 1|party==2
drop if can_id=="S2ME00059"
drop if can_id=="H8CA36097"
drop if can_id=="H8CA37137"
drop if can_id=="H2NY13088"
drop if can_id=="H0NY04057"
drop if can_id == "H2LA01075"
replace party=2 if can_id =="H2NY05084"
*Florida district 4 has IND, no DEM
replace can_party = "IND" if can_id=="H2FL04112"
replace  party=3 if  can_party=="IND"
*districts in which 2 people from the same party are competing
gen can_off_dis2=real( can_off_dis)
drop  can_off_dis_code
rename  can_off_dis2  can_off_dis
drop if can_off_sta=="LA" & can_off_dis==3
drop if can_off_sta=="LA" & can_off_dis==2
drop if can_off_sta=="CA" & can_off_dis==43
drop if can_off_sta=="CA" & can_off_dis==39
drop if can_off_sta=="CA" & can_off_dis==35
drop if can_off_sta=="CA" & can_off_dis==31
drop if can_off_sta=="CA" & can_off_dis==30
drop if can_off_sta=="CA" & can_off_dis==15
drop if can_off_sta=="CA" & can_off_dis==8

drop _merge

saveold spending_all_codes.dta, replace


*****************************************Spending Types*************************************

forvalues i=1(1)12   {
use spending_all_states.dta, clear

 keep if code==`i'
 collapse (sum)  dis_amo, by(can_id can_off can_off_sta can_off_dis day)
 gen cum_spend = .
 compress

foreach day of numlist 0/150 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend0- cum_spend150 {
replace cum_spend = `var' if cum_spend==.
}
drop cum_spend0- cum_spend150

foreach day of numlist 151/300 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend151- cum_spend300 {
replace cum_spend = `var' if cum_spend==.
}

drop cum_spend151- cum_spend300

foreach day of numlist 301/367 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend301- cum_spend367 {
replace cum_spend = `var' if cum_spend==.
}

drop cum_spend301- cum_spend367

*i need to fill in the missing day with the cumulative spending of the previous day
drop if missing(day)
drop dis_amo

reshape wide cum_spend, i(can_id can_off can_off_sta can_off_dis) j(day)
reshape long cum_spend, i(can_id can_off can_off_sta can_off_dis) j(day)
sort can_id day

/*replacing the missing value (on a day in which 0 was spent) with the closest 
previous cumulative spending value*/
by can_id: replace cum_spend = cum_spend[_n-1] if cum_spend==.
replace cum_spend= 0 if missing(cum_spend)

*detemining the beginning of the general election by the end of the primary
replace day = day+365
gen begin_day = .
replace begin_day = 1 if day==481 & can_off_sta=="AL"
replace begin_day = 1 if day==638 & can_off_sta=="AK"
replace begin_day = 1 if day==638 & can_off_sta=="AZ"
replace begin_day = 1 if day == 530 & can_off_sta=="AR"
replace begin_day = 1 if day == 523 & can_off_sta=="CA"
replace begin_day = 1 if day == 544 & can_off_sta=="CO"
replace begin_day = 1 if day == 603 & can_off_sta=="CT"
replace begin_day = 1 if day == 621 & can_off_sta=="DE"
replace begin_day = 1 if day == 595 & can_off_sta=="FL"
replace begin_day = 1 if day == 600 & can_off_sta=="GA"
replace begin_day = 1 if day == 590 & can_off_sta=="HI"
replace begin_day = 1 if day == 502 & can_off_sta=="ID"
replace begin_day = 1 if day == 446 & can_off_sta=="IL"
replace begin_day = 1 if day == 495 & can_off_sta=="IN"
replace begin_day = 1 if day == 523 & can_off_sta=="IA"
replace begin_day = 1 if day == 586 & can_off_sta=="KS"
replace begin_day = 1 if day == 530 & can_off_sta=="KY"
replace begin_day = 1 if day == 360 & can_off_sta=="LA"
replace begin_day = 1 if day == 530 & can_off_sta=="ME"
replace begin_day = 1 if day == 460 & can_off_sta=="MD"
replace begin_day = 1 if day == 616 & can_off_sta=="MA"
replace begin_day = 1 if day == 587 & can_off_sta=="MI"
replace begin_day = 1 if day == 593 & can_off_sta=="MN"
replace begin_day = 1 if day == 460 & can_off_sta=="MS"
replace begin_day = 1 if day == 586 & can_off_sta=="MO"
replace begin_day = 1 if day == 523 & can_off_sta=="MT"
replace begin_day = 1 if day == 502 & can_off_sta=="NE"
replace begin_day = 1 if day == 530 & can_off_sta=="NV"
replace begin_day = 1 if day == 621 & can_off_sta=="NH"
replace begin_day = 1 if day == 523 & can_off_sta=="NJ"
replace begin_day = 1 if day == 523 & can_off_sta=="NM"
replace begin_day = 1 if day == 544 & can_off_sta=="NY"
replace begin_day = 1 if day == 565 & can_off_sta=="NC"
replace begin_day = 1 if day == 530 & can_off_sta=="ND"
replace begin_day = 1 if day == 432 & can_off_sta=="OH"
replace begin_day = 1 if day == 607 & can_off_sta=="OK"
replace begin_day = 1 if day == 502 & can_off_sta=="OR"
replace begin_day = 1 if day == 481 & can_off_sta=="PA"
replace begin_day = 1 if day == 621 & can_off_sta=="RI"
replace begin_day = 1 if day == 544 & can_off_sta=="SC"
replace begin_day = 1 if day == 544 & can_off_sta=="SD"
replace begin_day = 1 if day == 591 & can_off_sta=="TN"
replace begin_day = 1 if day == 579 & can_off_sta=="TX"
replace begin_day = 1 if day == 544 & can_off_sta=="UT"
replace begin_day = 1 if day == 606 & can_off_sta=="VT"
replace begin_day = 1 if day == 530 & can_off_sta=="VA"
replace begin_day = 1 if day == 586 & can_off_sta=="WA"
replace begin_day = 1 if day == 495 & can_off_sta=="WV"
replace begin_day = 1 if day == 593 & can_off_sta=="WI"
replace begin_day = 1 if day == 600 & can_off_sta=="WY"

replace begin_day = 2 if day==676
replace begin_day = 3 if day==732

saveold code`i'.dta, replace

rename can_off_dis can_off_dis_code
*master FEC candidate data list taken from http://www.fec.gov/finance/disclosure/ftpdet.shtml#a2011_2012
merge m:1 can_id using cand_master_list.dta
keep if _merge ==1 | _merge==3
drop _merge

*merge with dataset that will identify whether the politician was a candidate in the general election
merge m:1 can_id using cand_list2.dta
*Some candidates are missing. Fixed below
replace candidate = 0 if can_id=="H2CA21159"
replace candidate = 0 if can_id=="H2MI01118"
replace candidate = 0 if can_id=="H2NV02239"
replace candidate = 0 if can_id=="H4IN02051"
replace candidate = 0 if can_id=="H6OH23033"

drop if missing( can_off_sta)

keep if candidate==1

*fixing mis-assigned candidates
replace can_party = "REP" if can_id=="H2NY05084"
replace can_party = "REP" if can_id=="H0GA07133"
replace can_party = "DEM" if can_id=="H2NY22113"
replace can_party = "REP" if can_id=="H2NY26171"
replace can_party = "REP" if can_id=="H2PA14084"
replace can_inc_cha_ope_sea="CHALLENGER" if can_id=="H2NY05084"
*generate party code (1=dem, 2=rep)
gen party = 1 if can_party=="DEM" | can_party=="DFL"
replace party = 2 if can_party=="REP"
keep if party== 1|party==2
drop if can_id=="S2ME00059"
drop if can_id=="H8CA36097"
drop if can_id=="H8CA37137"
drop if can_id=="H2NY13088"
drop if can_id=="H0NY04057"
drop if can_id == "H2LA01075"
replace party=2 if can_id =="H2NY05084"
*Florida district 4 has IND, no DEM
replace can_party = "IND" if can_id=="H2FL04112"
replace  party=3 if  can_party=="IND"
*districts in which 2 people from the same party are competing
gen can_off_dis2=real( can_off_dis)
drop  can_off_dis_code
rename  can_off_dis2  can_off_dis
drop if can_off_sta=="LA" & can_off_dis==3
drop if can_off_sta=="LA" & can_off_dis==2
drop if can_off_sta=="CA" & can_off_dis==43
drop if can_off_sta=="CA" & can_off_dis==39
drop if can_off_sta=="CA" & can_off_dis==35
drop if can_off_sta=="CA" & can_off_dis==31
drop if can_off_sta=="CA" & can_off_dis==30
drop if can_off_sta=="CA" & can_off_dis==15
drop if can_off_sta=="CA" & can_off_dis==8

drop _merge

saveold code`i'.dta, replace

}

*Almost forgot about spending that isn't coded (missing code variable)

use spending_all_states.dta, clear

 keep if code==.
 collapse (sum)  dis_amo, by(can_id can_off can_off_sta can_off_dis day)
 gen cum_spend = .
 compress

foreach day of numlist 0/150 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend0- cum_spend150 {
replace cum_spend = `var' if cum_spend==.
}
drop cum_spend0- cum_spend150

foreach day of numlist 151/300 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend151- cum_spend300 {
replace cum_spend = `var' if cum_spend==.
}

drop cum_spend151- cum_spend300

foreach day of numlist 301/367 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend301- cum_spend367 {
replace cum_spend = `var' if cum_spend==.
}

drop cum_spend301- cum_spend367

*i need to fill in the missing day with the cumulative spending of the previous day
drop if missing(day)
drop dis_amo

reshape wide cum_spend, i(can_id can_off can_off_sta can_off_dis) j(day)
reshape long cum_spend, i(can_id can_off can_off_sta can_off_dis) j(day)
sort can_id day

/*replacing the missing value (on a day in which 0 was spent) with the closest 
previous cumulative spending value*/
by can_id: replace cum_spend = cum_spend[_n-1] if cum_spend==.
replace cum_spend= 0 if missing(cum_spend)

*detemining the beginning of the general election by the end of the primary
replace day = day+365
gen begin_day = .
replace begin_day = 1 if day==481 & can_off_sta=="AL"
replace begin_day = 1 if day==638 & can_off_sta=="AK"
replace begin_day = 1 if day==638 & can_off_sta=="AZ"
replace begin_day = 1 if day == 530 & can_off_sta=="AR"
replace begin_day = 1 if day == 523 & can_off_sta=="CA"
replace begin_day = 1 if day == 544 & can_off_sta=="CO"
replace begin_day = 1 if day == 603 & can_off_sta=="CT"
replace begin_day = 1 if day == 621 & can_off_sta=="DE"
replace begin_day = 1 if day == 595 & can_off_sta=="FL"
replace begin_day = 1 if day == 600 & can_off_sta=="GA"
replace begin_day = 1 if day == 590 & can_off_sta=="HI"
replace begin_day = 1 if day == 502 & can_off_sta=="ID"
replace begin_day = 1 if day == 446 & can_off_sta=="IL"
replace begin_day = 1 if day == 495 & can_off_sta=="IN"
replace begin_day = 1 if day == 523 & can_off_sta=="IA"
replace begin_day = 1 if day == 586 & can_off_sta=="KS"
replace begin_day = 1 if day == 530 & can_off_sta=="KY"
replace begin_day = 1 if day == 360 & can_off_sta=="LA"
replace begin_day = 1 if day == 530 & can_off_sta=="ME"
replace begin_day = 1 if day == 460 & can_off_sta=="MD"
replace begin_day = 1 if day == 616 & can_off_sta=="MA"
replace begin_day = 1 if day == 587 & can_off_sta=="MI"
replace begin_day = 1 if day == 593 & can_off_sta=="MN"
replace begin_day = 1 if day == 460 & can_off_sta=="MS"
replace begin_day = 1 if day == 586 & can_off_sta=="MO"
replace begin_day = 1 if day == 523 & can_off_sta=="MT"
replace begin_day = 1 if day == 502 & can_off_sta=="NE"
replace begin_day = 1 if day == 530 & can_off_sta=="NV"
replace begin_day = 1 if day == 621 & can_off_sta=="NH"
replace begin_day = 1 if day == 523 & can_off_sta=="NJ"
replace begin_day = 1 if day == 523 & can_off_sta=="NM"
replace begin_day = 1 if day == 544 & can_off_sta=="NY"
replace begin_day = 1 if day == 565 & can_off_sta=="NC"
replace begin_day = 1 if day == 530 & can_off_sta=="ND"
replace begin_day = 1 if day == 432 & can_off_sta=="OH"
replace begin_day = 1 if day == 607 & can_off_sta=="OK"
replace begin_day = 1 if day == 502 & can_off_sta=="OR"
replace begin_day = 1 if day == 481 & can_off_sta=="PA"
replace begin_day = 1 if day == 621 & can_off_sta=="RI"
replace begin_day = 1 if day == 544 & can_off_sta=="SC"
replace begin_day = 1 if day == 544 & can_off_sta=="SD"
replace begin_day = 1 if day == 591 & can_off_sta=="TN"
replace begin_day = 1 if day == 579 & can_off_sta=="TX"
replace begin_day = 1 if day == 544 & can_off_sta=="UT"
replace begin_day = 1 if day == 606 & can_off_sta=="VT"
replace begin_day = 1 if day == 530 & can_off_sta=="VA"
replace begin_day = 1 if day == 586 & can_off_sta=="WA"
replace begin_day = 1 if day == 495 & can_off_sta=="WV"
replace begin_day = 1 if day == 593 & can_off_sta=="WI"
replace begin_day = 1 if day == 600 & can_off_sta=="WY"

replace begin_day = 2 if day==676
replace begin_day = 3 if day==732

saveold code0.dta, replace

rename can_off_dis can_off_dis_code
*master FEC candidate data list taken from http://www.fec.gov/finance/disclosure/ftpdet.shtml#a2011_2012
merge m:1 can_id using cand_master_list.dta
keep if _merge ==1 | _merge==3
drop _merge

*merge with dataset that will identify whether the politician was a candidate in the general election
merge m:1 can_id using cand_list2.dta
*Some candidates are missing. Fixed below
replace candidate = 0 if can_id=="H2CA21159"
replace candidate = 0 if can_id=="H2MI01118"
replace candidate = 0 if can_id=="H2NV02239"
replace candidate = 0 if can_id=="H4IN02051"
replace candidate = 0 if can_id=="H6OH23033"

drop if missing( can_off_sta)

keep if candidate==1

*fixing mis-assigned candidates
replace can_party = "REP" if can_id=="H2NY05084"
replace can_party = "REP" if can_id=="H0GA07133"
replace can_party = "DEM" if can_id=="H2NY22113"
replace can_party = "REP" if can_id=="H2NY26171"
replace can_party = "REP" if can_id=="H2PA14084"
replace can_inc_cha_ope_sea="CHALLENGER" if can_id=="H2NY05084"
*generate party code (1=dem, 2=rep)
gen party = 1 if can_party=="DEM" | can_party=="DFL"
replace party = 2 if can_party=="REP"
keep if party== 1|party==2
drop if can_id=="S2ME00059"
drop if can_id=="H8CA36097"
drop if can_id=="H8CA37137"
drop if can_id=="H2NY13088"
drop if can_id=="H0NY04057"
drop if can_id == "H2LA01075"
replace party=2 if can_id =="H2NY05084"
*Florida district 4 has IND, no DEM
replace can_party = "IND" if can_id=="H2FL04112"
replace  party=3 if  can_party=="IND"
*districts in which 2 people from the same party are competing
gen can_off_dis2=real( can_off_dis)
drop  can_off_dis_code
rename  can_off_dis2  can_off_dis
drop if can_off_sta=="LA" & can_off_dis==3
drop if can_off_sta=="LA" & can_off_dis==2
drop if can_off_sta=="CA" & can_off_dis==43
drop if can_off_sta=="CA" & can_off_dis==39
drop if can_off_sta=="CA" & can_off_dis==35
drop if can_off_sta=="CA" & can_off_dis==31
drop if can_off_sta=="CA" & can_off_dis==30
drop if can_off_sta=="CA" & can_off_dis==15
drop if can_off_sta=="CA" & can_off_dis==8

drop _merge

save code0.dta, replace


*********************SPENDING PATTERNS*************************
*creating aggregate spending by incumbent/challenger/open category
*dropping uncontested elections. The value of sum below will be equal to 368 if there is only 1 candidate
keep if var2=="W"

gen uncontested= can_name=="ROBY, MARTHA"|can_name=="ROSS, DENNIS ALAN"|can_name=="WILSON, FREDERICA S" ///
|can_name=="WESTMORELAND, LYNN A."|can_name=="SCOTT, JAMES AUSTIN"|can_name=="BROUN, PAUL COLLINS"| ///
can_name=="HUELSKAMP, TIMOTHY A REPRESENTA"|can_name=="NEAL, RICHARD E MR."|can_name=="MCGOVERN, JIM"| ///
can_name=="SERRANO, JOSE E"|can_name=="BOEHNER, JOHN A"|can_name=="FUDGE, MARCIA L"|can_name=="WILSON, ADDISON (JOE) GRAVES"| ///
can_name=="JOHNSON, SAMUEL ROBERT"

collapse (mean) cum_spend, by( uncontested day)

reshape wide   cum_spend, i( day) j( unc)

label var  cum_spend1 "Uncontested"
label var  cum_spend0 "Contested"

replace  cum_spend0= cum_spend0/1000
replace  cum_spend1= cum_spend1/1000
replace  day =  day-365

line cum_spend0  cum_spend1 day, ///
  xline(80 245 310, lcolor(black) lpattern(dash)) ///
  ytitle("Cumulative Spending (in thousands)") ///
  xlabel(0 "Year Begin" 60 "March 1"  245 "Sept. 1" 310 "Election Day" ) ///
  legend(pos(9) ring(0) col(1)) ///
  xtitle("Day of year (2012)") 
  graph save graph2, replace
  


 ******************MERGING*******************
 /*i need to merge this into the ANES data twice. once for the 
day of the survey, and another for the pre, post, and final 
election spending*/

use spending_all_codes.dta, clear

*this one if for the day of the preelection survey
drop can_id var2 can_party candidate can_name
reshape wide  cum_spend  can_inc_cha_ope_sea, i(can_off can_off_sta can_off_dis ///
day begin_day) j(party)

*creating a unique identifier that will provides the day and district
egen ID = concat(can_off can_off_sta can_off_dis day), punc(-)

keep cum_spend1 can_inc_cha_ope_sea1 cum_spend2 can_inc_cha_ope_sea2 cum_spend3 can_inc_cha_ope_sea3 ID

replace cum_spend1 = 0 if (can_inc_cha_ope_sea1=="OPEN"|can_inc_cha_ope_sea1=="INCUMBENT"| ///
can_inc_cha_ope_sea1=="CHALLENGER") & missing(cum_spend1)

replace cum_spend2 = 0 if (can_inc_cha_ope_sea2=="OPEN"|can_inc_cha_ope_sea2=="INCUMBENT"| ///
can_inc_cha_ope_sea2=="CHALLENGER") & missing(cum_spend2)

gen day = substr( ID, 8,10)
gen day1= real(day)
replace day1 = abs(day1)
keep if day1>610 & day1<690
drop day day1
save day_of_all.dta, replace

*this is for election-day and final spending
use spending_all_codes.dta, clear

keep if  begin_day!=.
drop day

reshape wide  cum_spend, i( can_id can_off can_off_sta can_off_dis ///
can_party can_inc_cha_ope_sea candidate party) j( begin_day)

rename cum_spend1 cum_spend1_
rename cum_spend2 cum_spend2_
rename cum_spend3 cum_spend3_

drop can_id can_party  candidate var2 can_name

reshape wide cum_spend1_ cum_spend2_ cum_spend3_  can_inc_cha_ope_sea, ///
i(can_off can_off_sta can_off_dis ) j(party)


egen ID2 = concat(can_off  can_off_sta can_off_dis), punc(-)

saveold spend_final_all.dta, replace

/*use anes_timeseries_2012_stata12.dta, clear
compress

*dropping restricted or irrelavent data
drop prevote_regpty_oth relig_group relig_groupna relig_denom- relig_jewishna ///
dem_birthmo- dem_age_r_x dem_edu dem_edu_oth dem_edusp dem_edusp_oth dem_milnow ///
dem_retiremo- dem_retireyr dem_hispoth dem_hispmost_oth- dem_racecps_3rd ///
dem_racecps_native- dem_racecps_pacif dem_racecps_oth dem_ethnic dem_mosteth ///
dem_nativity_oth dem_hispcntry- dem_arr dem_cit dem3_moscomm- dem3_prevcty ///
dem3_namechyr dem3_add5yr_state inc_totinc mobilpo_ctbcndpty_oth ///
mobilpo_ctbwhpty mobilpo_ctbwhpty_oth postvote_regpty_oth cses_ptyclost_oth ///
ident_nativeid- ident_pacifid incpo_totinc sample_countyfips sample_countyname ///
sample_ftf_tract sample_web_cbsa sample_web_mktarea dwell_winsign_dsc ///
dwell_frontsign_dsc dwell_religmsg_spec dwell_structure_oth dwell_strctsize_oth ///
dwell_block_resid_oth pre_iwrdesc_age pre_iwrdesc_educ post_iwrdesc_age ///
post_iwrdesc_educ profile_educdegree profile_hhchild0to1- profile_hhchild13to17 ///
paprofile_mip_oth paprofile_pres2008_oth paprofile_othpid ///
paprofile_ancestry_1- paprofile_rlang_3 paprofile_otherslang_1- paprofile_otherslang_3 ///
paprofile_whencit paprofile_priorcit paprofile_2ndcit paprofile_transgender ///
paprofile_milit paprofile_religion_oth paprofile_jewish paprofile_jewishparent ///
paprofile_othergun_type profile_race1- profile_race3 profile_birthmonth- profile_birthyear ///
version prevote_vtpresdec_dkrf congapp_job_x presapp_job_x  presapp_econ_x presapp_foreign_x ///
presapp_health_x presapp_war_x  ptylik_lwhatdp_txt ptylik_dwhatdp_txt ptylik_lwhatrp_txt ///
ptylik_dwhatrp_txt finance_finpast_x finance_finnext_x health_2010hcr_x  ineq_incgap_x ///
econ_ecpast_x econ_ecnext_x econ_unpast_x  preswin_dutychoice_x tea_supp_x aa_uni_x aa_work_x ///
gayrt_discrev_x gayrt_discstd_x gayrt_milrev_x gayrt_milstd_x penalty_favopp_x ///
relig_ident_1st- relig_ident_3rd  dem_edugroup dem_eduspgroup dem_emptype_1st- dem_emptype_4th ///
dem_empstatus_2digitfinal dem_empstatus_1digitfinal dem_retireperiod dem_emptypesp_1st- dem_emptypesp_4th ///
dem_unionwho_1st dem_unionwho_2nd dem_hisptyp_1st dem_hisptyp_2nd  dem_raceeth ///
dem_numeth dem2_schenrl_1st- dem2_schenrl_3rd  casistart_iwrset4- selfgend_gender ///
medsrc_campsrcs_1st- medsrc_campsrcs_5th  medsrc_tvprog_01_screen -  medsrc_tvprog_64_screen ///
pre_iwrobs_skintone  postvote_regpre- postvote_prestatus_nonvtr postvote_regist_noaddr- postvote_reg_samestate ///
presvote2012_x  postvote_vtpresdec_dkrf postvote_senhhstate postvote_senregstate postvote_govhhstate ///
postvote_govregstate ofcrec_speakerpr ofcrec_vppr  ofcrec_pmukpr ofcrec_cjpr hseinc_approval_x mip_prob1_txt ///
mip_prob2_txt mip_prob3_txt mip_mostprob_txt wpres_gdbd_x budget_deficit_x milln_milltax_x fairjob_opin_x ///
abort_dpc4_othtxt abort_rpc4_othtxt abort_dem4_othtxt abort_rep4_othtxt abort_health_x abort_fatal_x ///
abort_incest_x abort_rape_x abort_bd_x abort_fin_x  abort_sex_x abort_choice_x outsource_enc_x ssinv_invest_x ///
neonull_strule_x tarp_favopp_x scourt_remove_x scourt_elim_x  women_bond_x women_works_x modsex_media_x ///
modsex_oppor_x aapost_hire_x cog_opin_x  cses_ptycont_1st- cses_ptycont_5th  cses_ptycont_othtxt ///
casistartpo_iwrset4 casistartpo_iwrset6  sample_ftf_replicate- sample_ftf_timezone ///
pre_admin_iwrecorded pre_admin_ltr_notif- pre_admin_ltr_nocontdate ///
post_admin_iwrecorded post_admin_ltr_notif - paprofile_comments sample_web_statusmsa ///
prevote_senhhstate prevote_senregstate sample_ftfpsu  rstype_violcath- rstype_patrnonrel ///
gend_gendobs  prevote_ballot_color prevote_regist_noaddr- prevote_reg_samestate ///
prevote_govhhstate prevote_govregstate

save anes_timeseries_2012_stata12_trim.dta, replace*/

use anes_timeseries_2012_stata12_trim.dta, clear


/*isolating the variables that I need
order prevote_regint prevote_voted prevote_inths prevote_inthsbc prevote_inthsnobc ///
prevote_intsen prevote_intsenbc prevote_intsennobc prevote_intgov prevote_intgovbc ///
prevote_intgovnobc postvote_rvote postvote_hsvtbc postvote_votesen ///
postvote_vtsenbc postvote_vtsennobc postvote_votegov postvote_vtgovbc postvote_vtgovnobc ///
sample_stfips sample_district sample_state pre_admin_iwdatebeg*/

*prevote_votehs is people who already vote in house
*prevote_votesen is people who already voted in senate
 
*generating days code
gen year = 2012
gen month = substr( pre_admin_iwdatebeg, 5,6)
replace month = substr(month,1,2)
gen day = substr( pre_admin_iwdatebeg, 7,8)
egen dis_dat = concat(year month day), punc(-)

rename day day_code
gen day =.
replace day = 366 if dis_dat== "2012-01-01"
replace day = 367 if dis_dat== "2012-01-02"
replace day = 368 if dis_dat== "2012-01-03"
replace day = 369 if dis_dat== "2012-01-04"
replace day = 370 if dis_dat== "2012-01-05"
replace day = 371 if dis_dat== "2012-01-06"
replace day = 372 if dis_dat== "2012-01-07"
replace day = 373 if dis_dat== "2012-01-08"
replace day = 374 if dis_dat== "2012-01-09"
replace day = 375 if dis_dat== "2012-01-10"
replace day = 376 if dis_dat== "2012-01-11"
replace day = 377 if dis_dat== "2012-01-12"
replace day = 378 if dis_dat== "2012-01-13"
replace day = 379 if dis_dat== "2012-01-14"
replace day = 380 if dis_dat== "2012-01-15"
replace day = 381 if dis_dat== "2012-01-16"
replace day = 382 if dis_dat== "2012-01-17"
replace day = 383 if dis_dat== "2012-01-18"
replace day = 384 if dis_dat== "2012-01-19"
replace day = 385 if dis_dat== "2012-01-20"
replace day = 386 if dis_dat== "2012-01-21"
replace day = 387 if dis_dat== "2012-01-22"
replace day = 388 if dis_dat== "2012-01-23"
replace day = 389 if dis_dat== "2012-01-24"
replace day = 390 if dis_dat== "2012-01-25"
replace day = 391 if dis_dat== "2012-01-26"
replace day = 392 if dis_dat== "2012-01-27"
replace day = 393 if dis_dat== "2012-01-28"
replace day = 394 if dis_dat== "2012-01-29"
replace day = 395 if dis_dat== "2012-01-30"
replace day = 396 if dis_dat== "2012-01-31"
replace day = 397 if dis_dat== "2012-02-01"
replace day = 398 if dis_dat== "2012-02-02"
replace day = 399 if dis_dat== "2012-02-03"
replace day = 400 if dis_dat== "2012-02-04"
replace day = 401 if dis_dat== "2012-02-05"
replace day = 402 if dis_dat== "2012-02-06"
replace day = 403 if dis_dat== "2012-02-07"
replace day = 404 if dis_dat== "2012-02-08"
replace day = 405 if dis_dat== "2012-02-09"
replace day = 406 if dis_dat== "2012-02-10"
replace day = 407 if dis_dat== "2012-02-11"
replace day = 408 if dis_dat== "2012-02-12"
replace day = 409 if dis_dat== "2012-02-13"
replace day = 410 if dis_dat== "2012-02-14"
replace day = 411 if dis_dat== "2012-02-15"
replace day = 412 if dis_dat== "2012-02-16"
replace day = 413 if dis_dat== "2012-02-17"
replace day = 414 if dis_dat== "2012-02-18"
replace day = 415 if dis_dat== "2012-02-19"
replace day = 416 if dis_dat== "2012-02-20"
replace day = 417 if dis_dat== "2012-02-21"
replace day = 418 if dis_dat== "2012-02-22"
replace day = 419 if dis_dat== "2012-02-23"
replace day = 420 if dis_dat== "2012-02-24"
replace day = 421 if dis_dat== "2012-02-25"
replace day = 422 if dis_dat== "2012-02-26"
replace day = 423 if dis_dat== "2012-02-27"
replace day = 424 if dis_dat== "2012-02-28"
replace day = 425 if dis_dat== "2012-02-29"
replace day = 426 if dis_dat== "2012-03-01"
replace day = 427 if dis_dat== "2012-03-02"
replace day = 428 if dis_dat== "2012-03-03"
replace day = 429 if dis_dat== "2012-03-04"
replace day = 430 if dis_dat== "2012-03-05"
replace day = 431 if dis_dat== "2012-03-06"
replace day = 432 if dis_dat== "2012-03-07"
replace day = 433 if dis_dat== "2012-03-08"
replace day = 434 if dis_dat== "2012-03-09"
replace day = 435 if dis_dat== "2012-03-10"
replace day = 436 if dis_dat== "2012-03-11"
replace day = 437 if dis_dat== "2012-03-12"
replace day = 438 if dis_dat== "2012-03-13"
replace day = 439 if dis_dat== "2012-03-14"
replace day = 440 if dis_dat== "2012-03-15"
replace day = 441 if dis_dat== "2012-03-16"
replace day = 442 if dis_dat== "2012-03-17"
replace day = 443 if dis_dat== "2012-03-18"
replace day = 444 if dis_dat== "2012-03-19"
replace day = 445 if dis_dat== "2012-03-20"
replace day = 446 if dis_dat== "2012-03-21"
replace day = 447 if dis_dat== "2012-03-22"
replace day = 448 if dis_dat== "2012-03-23"
replace day = 449 if dis_dat== "2012-03-24"
replace day = 450 if dis_dat== "2012-03-25"
replace day = 451 if dis_dat== "2012-03-26"
replace day = 452 if dis_dat== "2012-03-27"
replace day = 453 if dis_dat== "2012-03-28"
replace day = 454 if dis_dat== "2012-03-29"
replace day = 455 if dis_dat== "2012-03-30"
replace day = 456 if dis_dat== "2012-03-31"
replace day = 457 if dis_dat== "2012-04-01"
replace day = 458 if dis_dat== "2012-04-02"
replace day = 459 if dis_dat== "2012-04-03"
replace day = 460 if dis_dat== "2012-04-04"
replace day = 461 if dis_dat== "2012-04-05"
replace day = 462 if dis_dat== "2012-04-06"
replace day = 463 if dis_dat== "2012-04-07"
replace day = 464 if dis_dat== "2012-04-08"
replace day = 465 if dis_dat== "2012-04-09"
replace day = 466 if dis_dat== "2012-04-10"
replace day = 467 if dis_dat== "2012-04-11"
replace day = 468 if dis_dat== "2012-04-12"
replace day = 469 if dis_dat== "2012-04-13"
replace day = 470 if dis_dat== "2012-04-14"
replace day = 471 if dis_dat== "2012-04-15"
replace day = 472 if dis_dat== "2012-04-16"
replace day = 473 if dis_dat== "2012-04-17"
replace day = 474 if dis_dat== "2012-04-18"
replace day = 475 if dis_dat== "2012-04-19"
replace day = 476 if dis_dat== "2012-04-20"
replace day = 477 if dis_dat== "2012-04-21"
replace day = 478 if dis_dat== "2012-04-22"
replace day = 479 if dis_dat== "2012-04-23"
replace day = 480 if dis_dat== "2012-04-24"
replace day = 481 if dis_dat== "2012-04-25"
replace day = 482 if dis_dat== "2012-04-26"
replace day = 483 if dis_dat== "2012-04-27"
replace day = 484 if dis_dat== "2012-04-28"
replace day = 485 if dis_dat== "2012-04-29"
replace day = 486 if dis_dat== "2012-04-30"
replace day = 487 if dis_dat== "2012-05-01"
replace day = 488 if dis_dat== "2012-05-02"
replace day = 489 if dis_dat== "2012-05-03"
replace day = 490 if dis_dat== "2012-05-04"
replace day = 491 if dis_dat== "2012-05-05"
replace day = 492 if dis_dat== "2012-05-06"
replace day = 493 if dis_dat== "2012-05-07"
replace day = 494 if dis_dat== "2012-05-08"
replace day = 495 if dis_dat== "2012-05-09"
replace day = 496 if dis_dat== "2012-05-10"
replace day = 497 if dis_dat== "2012-05-11"
replace day = 498 if dis_dat== "2012-05-12"
replace day = 499 if dis_dat== "2012-05-13"
replace day = 500 if dis_dat== "2012-05-14"
replace day = 501 if dis_dat== "2012-05-15"
replace day = 502 if dis_dat== "2012-05-16"
replace day = 503 if dis_dat== "2012-05-17"
replace day = 504 if dis_dat== "2012-05-18"
replace day = 505 if dis_dat== "2012-05-19"
replace day = 506 if dis_dat== "2012-05-20"
replace day = 507 if dis_dat== "2012-05-21"
replace day = 508 if dis_dat== "2012-05-22"
replace day = 509 if dis_dat== "2012-05-23"
replace day = 510 if dis_dat== "2012-05-24"
replace day = 511 if dis_dat== "2012-05-25"
replace day = 512 if dis_dat== "2012-05-26"
replace day = 513 if dis_dat== "2012-05-27"
replace day = 514 if dis_dat== "2012-05-28"
replace day = 515 if dis_dat== "2012-05-29"
replace day = 516 if dis_dat== "2012-05-30"
replace day = 517 if dis_dat== "2012-05-31"
replace day = 518 if dis_dat== "2012-06-01"
replace day = 519 if dis_dat== "2012-06-02"
replace day = 520 if dis_dat== "2012-06-03"
replace day = 521 if dis_dat== "2012-06-04"
replace day = 522 if dis_dat== "2012-06-05"
replace day = 523 if dis_dat== "2012-06-06"
replace day = 524 if dis_dat== "2012-06-07"
replace day = 525 if dis_dat== "2012-06-08"
replace day = 526 if dis_dat== "2012-06-09"
replace day = 527 if dis_dat== "2012-06-10"
replace day = 528 if dis_dat== "2012-06-11"
replace day = 529 if dis_dat== "2012-06-12"
replace day = 530 if dis_dat== "2012-06-13"
replace day = 531 if dis_dat== "2012-06-14"
replace day = 532 if dis_dat== "2012-06-15"
replace day = 533 if dis_dat== "2012-06-16"
replace day = 534 if dis_dat== "2012-06-17"
replace day = 535 if dis_dat== "2012-06-18"
replace day = 536 if dis_dat== "2012-06-19"
replace day = 537 if dis_dat== "2012-06-20"
replace day = 538 if dis_dat== "2012-06-21"
replace day = 539 if dis_dat== "2012-06-22"
replace day = 540 if dis_dat== "2012-06-23"
replace day = 541 if dis_dat== "2012-06-24"
replace day = 542 if dis_dat== "2012-06-25"
replace day = 543 if dis_dat== "2012-06-26"
replace day = 544 if dis_dat== "2012-06-27"
replace day = 545 if dis_dat== "2012-06-28"
replace day = 546 if dis_dat== "2012-06-29"
replace day = 547 if dis_dat== "2012-06-30"
replace day = 548 if dis_dat== "2012-07-01"
replace day = 549 if dis_dat== "2012-07-02"
replace day = 550 if dis_dat== "2012-07-03"
replace day = 551 if dis_dat== "2012-07-04"
replace day = 552 if dis_dat== "2012-07-05"
replace day = 553 if dis_dat== "2012-07-06"
replace day = 554 if dis_dat== "2012-07-07"
replace day = 555 if dis_dat== "2012-07-08"
replace day = 556 if dis_dat== "2012-07-09"
replace day = 557 if dis_dat== "2012-07-10"
replace day = 558 if dis_dat== "2012-07-11"
replace day = 559 if dis_dat== "2012-07-12"
replace day = 560 if dis_dat== "2012-07-13"
replace day = 561 if dis_dat== "2012-07-14"
replace day = 562 if dis_dat== "2012-07-15"
replace day = 563 if dis_dat== "2012-07-16"
replace day = 564 if dis_dat== "2012-07-17"
replace day = 565 if dis_dat== "2012-07-18"
replace day = 566 if dis_dat== "2012-07-19"
replace day = 567 if dis_dat== "2012-07-20"
replace day = 568 if dis_dat== "2012-07-21"
replace day = 569 if dis_dat== "2012-07-22"
replace day = 570 if dis_dat== "2012-07-23"
replace day = 571 if dis_dat== "2012-07-24"
replace day = 572 if dis_dat== "2012-07-25"
replace day = 573 if dis_dat== "2012-07-26"
replace day = 574 if dis_dat== "2012-07-27"
replace day = 575 if dis_dat== "2012-07-28"
replace day = 576 if dis_dat== "2012-07-29"
replace day = 577 if dis_dat== "2012-07-30"
replace day = 578 if dis_dat== "2012-07-31"
replace day = 579 if dis_dat== "2012-08-01"
replace day = 580 if dis_dat== "2012-08-02"
replace day = 581 if dis_dat== "2012-08-03"
replace day = 582 if dis_dat== "2012-08-04"
replace day = 583 if dis_dat== "2012-08-05"
replace day = 584 if dis_dat== "2012-08-06"
replace day = 585 if dis_dat== "2012-08-07"
replace day = 586 if dis_dat== "2012-08-08"
replace day = 587 if dis_dat== "2012-08-09"
replace day = 588 if dis_dat== "2012-08-10"
replace day = 589 if dis_dat== "2012-08-11"
replace day = 590 if dis_dat== "2012-08-12"
replace day = 591 if dis_dat== "2012-08-13"
replace day = 592 if dis_dat== "2012-08-14"
replace day = 593 if dis_dat== "2012-08-15"
replace day = 594 if dis_dat== "2012-08-16"
replace day = 595 if dis_dat== "2012-08-17"
replace day = 596 if dis_dat== "2012-08-18"
replace day = 597 if dis_dat== "2012-08-19"
replace day = 598 if dis_dat== "2012-08-20"
replace day = 599 if dis_dat== "2012-08-21"
replace day = 600 if dis_dat== "2012-08-22"
replace day = 601 if dis_dat== "2012-08-23"
replace day = 602 if dis_dat== "2012-08-24"
replace day = 603 if dis_dat== "2012-08-25"
replace day = 604 if dis_dat== "2012-08-26"
replace day = 605 if dis_dat== "2012-08-27"
replace day = 606 if dis_dat== "2012-08-28"
replace day = 607 if dis_dat== "2012-08-29"
replace day = 608 if dis_dat== "2012-08-30"
replace day = 609 if dis_dat== "2012-08-31"
replace day = 610 if dis_dat== "2012-09-01"
replace day = 611 if dis_dat== "2012-09-02"
replace day = 612 if dis_dat== "2012-09-03"
replace day = 613 if dis_dat== "2012-09-04"
replace day = 614 if dis_dat== "2012-09-05"
replace day = 615 if dis_dat== "2012-09-06"
replace day = 616 if dis_dat== "2012-09-07"
replace day = 617 if dis_dat== "2012-09-08"
replace day = 618 if dis_dat== "2012-09-09"
replace day = 619 if dis_dat== "2012-09-10"
replace day = 620 if dis_dat== "2012-09-11"
replace day = 621 if dis_dat== "2012-09-12"
replace day = 622 if dis_dat== "2012-09-13"
replace day = 623 if dis_dat== "2012-09-14"
replace day = 624 if dis_dat== "2012-09-15"
replace day = 625 if dis_dat== "2012-09-16"
replace day = 626 if dis_dat== "2012-09-17"
replace day = 627 if dis_dat== "2012-09-18"
replace day = 628 if dis_dat== "2012-09-19"
replace day = 629 if dis_dat== "2012-09-20"
replace day = 630 if dis_dat== "2012-09-21"
replace day = 631 if dis_dat== "2012-09-22"
replace day = 632 if dis_dat== "2012-09-23"
replace day = 633 if dis_dat== "2012-09-24"
replace day = 634 if dis_dat== "2012-09-25"
replace day = 635 if dis_dat== "2012-09-26"
replace day = 636 if dis_dat== "2012-09-27"
replace day = 637 if dis_dat== "2012-09-28"
replace day = 638 if dis_dat== "2012-09-29"
replace day = 639 if dis_dat== "2012-09-30"
replace day = 640 if dis_dat== "2012-10-01"
replace day = 641 if dis_dat== "2012-10-02"
replace day = 642 if dis_dat== "2012-10-03"
replace day = 643 if dis_dat== "2012-10-04"
replace day = 644 if dis_dat== "2012-10-05"
replace day = 645 if dis_dat== "2012-10-06"
replace day = 646 if dis_dat== "2012-10-07"
replace day = 647 if dis_dat== "2012-10-08"
replace day = 648 if dis_dat== "2012-10-09"
replace day = 649 if dis_dat== "2012-10-10"
replace day = 650 if dis_dat== "2012-10-11"
replace day = 651 if dis_dat== "2012-10-12"
replace day = 652 if dis_dat== "2012-10-13"
replace day = 653 if dis_dat== "2012-10-14"
replace day = 654 if dis_dat== "2012-10-15"
replace day = 655 if dis_dat== "2012-10-16"
replace day = 656 if dis_dat== "2012-10-17"
replace day = 657 if dis_dat== "2012-10-18"
replace day = 658 if dis_dat== "2012-10-19"
replace day = 659 if dis_dat== "2012-10-20"
replace day = 660 if dis_dat== "2012-10-21"
replace day = 661 if dis_dat== "2012-10-22"
replace day = 662 if dis_dat== "2012-10-23"
replace day = 663 if dis_dat== "2012-10-24"
replace day = 664 if dis_dat== "2012-10-25"
replace day = 665 if dis_dat== "2012-10-26"
replace day = 666 if dis_dat== "2012-10-27"
replace day = 667 if dis_dat== "2012-10-28"
replace day = 668 if dis_dat== "2012-10-29"
replace day = 669 if dis_dat== "2012-10-30"
replace day = 670 if dis_dat== "2012-10-31"
replace day = 671 if dis_dat== "2012-11-01"
replace day = 672 if dis_dat== "2012-11-02"
replace day = 673 if dis_dat== "2012-11-03"
replace day = 674 if dis_dat== "2012-11-04"
replace day = 675 if dis_dat== "2012-11-05"
replace day = 676 if dis_dat== "2012-11-06"
replace day = 677 if dis_dat== "2012-11-07"
replace day = 678 if dis_dat== "2012-11-08"
replace day = 679 if dis_dat== "2012-11-09"
replace day = 680 if dis_dat== "2012-11-10"
replace day = 681 if dis_dat== "2012-11-11"
replace day = 682 if dis_dat== "2012-11-12"
replace day = 683 if dis_dat== "2012-11-13"
replace day = 684 if dis_dat== "2012-11-14"
replace day = 685 if dis_dat== "2012-11-15"
replace day = 686 if dis_dat== "2012-11-16"
replace day = 687 if dis_dat== "2012-11-17"
replace day = 688 if dis_dat== "2012-11-18"
replace day = 689 if dis_dat== "2012-11-19"
replace day = 690 if dis_dat== "2012-11-20"
replace day = 691 if dis_dat== "2012-11-21"
replace day = 692 if dis_dat== "2012-11-22"
replace day = 693 if dis_dat== "2012-11-23"
replace day = 694 if dis_dat== "2012-11-24"
replace day = 695 if dis_dat== "2012-11-25"
replace day = 696 if dis_dat== "2012-11-26"
replace day = 697 if dis_dat== "2012-11-27"
replace day = 698 if dis_dat== "2012-11-28"
replace day = 699 if dis_dat== "2012-11-29"
replace day = 700 if dis_dat== "2012-11-30"
replace day = 701 if dis_dat== "2012-12-01"
replace day = 702 if dis_dat== "2012-12-02"
replace day = 703 if dis_dat== "2012-12-03"
replace day = 704 if dis_dat== "2012-12-04"
replace day = 705 if dis_dat== "2012-12-05"
replace day = 706 if dis_dat== "2012-12-06"
replace day = 707 if dis_dat== "2012-12-07"
replace day = 708 if dis_dat== "2012-12-08"
replace day = 709 if dis_dat== "2012-12-09"
replace day = 710 if dis_dat== "2012-12-10"
replace day = 711 if dis_dat== "2012-12-11"
replace day = 712 if dis_dat== "2012-12-12"
replace day = 713 if dis_dat== "2012-12-13"
replace day = 714 if dis_dat== "2012-12-14"
replace day = 715 if dis_dat== "2012-12-15"
replace day = 716 if dis_dat== "2012-12-16"
replace day = 717 if dis_dat== "2012-12-17"
replace day = 718 if dis_dat== "2012-12-18"
replace day = 719 if dis_dat== "2012-12-19"
replace day = 720 if dis_dat== "2012-12-20"
replace day = 721 if dis_dat== "2012-12-21"
replace day = 722 if dis_dat== "2012-12-22"
replace day = 723 if dis_dat== "2012-12-23"
replace day = 724 if dis_dat== "2012-12-24"
replace day = 725 if dis_dat== "2012-12-25"
replace day = 726 if dis_dat== "2012-12-26"
replace day = 727 if dis_dat== "2012-12-27"
replace day = 728 if dis_dat== "2012-12-28"
replace day = 729 if dis_dat== "2012-12-29"
replace day = 730 if dis_dat== "2012-12-30"
replace day = 731 if dis_dat== "2012-12-31"

*dropping all senate questions
drop   prevote_votesen- prevote_senprefnobc  postvote_votesen- postvote_senprefnobc

*renaming cong. variables to generic variables

*PRE: Did R vote for U.S. House of |Representatives 
rename  prevote_votehs early_vote
*PRE: For whom did R vote for U.S. House (BC)
rename prevote_hsvtbc early_vote_bc
*PRE: For whom did R vote for U.S. House (NO-BC)
rename prevote_hsvtnobc early_vote_nobc
rename prevote_inths prevote_intent
rename prevote_inthsbc prevote_intent_bc
rename prevote_inthsnobc prevote_intent_nobc
*PRE: Does R prefer U.S. House | candidate (Non-voters
rename prevote_hspref prevote_pref
rename prevote_hsprefbc prevote_pref_bc
rename prevote_hsprefnobc prevote_pref_nobc

rename  postvote_votehs  postvote
rename  postvote_hsvtbc postvote_bc
rename  postvote_hsvtnobc postvote_nobc
rename  postvote_hspref post_pref
rename  postvote_hsprefbc post_pref_bc
rename  postvote_hsprefnobc post_pref_nobc
gen str2 house = "H"

save anes_house.dta, replace

/*egen ID = concat(house sample_state sample_district day), punc(-)

*dropping those who already voted
drop if prevote_voted==1

merge m:1 ID using day_of_all.dta

keep if _merge==3
drop _merge

egen ID2 = concat(house sample_state sample_district), punc(-)
merge m:1 ID2 using spend_final_all.dta

label var  cum_spend1 "Dem Spend Today"
label var  cum_spend2 "Rep Spend Today"
label var  cum_spend1_1 "Dem Spend Beginning"
label var  cum_spend2_1 "Dem Spend Election"
label var  cum_spend3_1 "Dem Spend Final"

label var  cum_spend1_2 "Rep Spend Beginning"
label var  cum_spend2_2 "Rep Spend Election"
label var  cum_spend3_2 "Rep Spend Final"

gen first_rep = cum_spend2 - cum_spend1_2
replace first_rep = 0 if first_rep<0
gen second_rep = cum_spend2_2 - cum_spend1_2

gen first_dem = cum_spend1 - cum_spend1_1
replace first_dem = 0 if first_dem<0
gen second_dem = cum_spend2_1 - cum_spend1_1
replace second_dem = 0 if second_dem<0


gen pre_vote = 1 if  prevote_intent_bc==2
replace pre_vote = 1 if prevote_intent_nobc==82
replace pre_vote = -1 if prevote_intent_nobc==81
replace pre_vote = -1 if prevote_intent_bc==1
replace pre_vote = 0 if prevote_intent_bc==-8| prevote_intent_bc>2
replace pre_vote = 0 if prevote_intent==2
replace pre_vote = 0 if prevote_intent_nobc==-8|prevote_intent_nobc==87

gen post_vote = 1 if postvote_bc == 2
replace post_vote = -1 if postvote_bc == 1
replace post_vote = 0 if postvote_bc >2
replace post_vote = 0 if postvote==2

gen dependent = post_vote-pre_vote

gen independent = second_rep - first_rep - second_dem + first_dem
gen independent2 = ln(second_rep+1) - ln(first_rep+1) - ln(second_dem+1) ///
 + ln(first_dem +1)

 *replacing missing values with 0
replace second_rep=0 if missing(second_rep)
replace second_dem=0 if missing(second_dem)
replace first_rep=0 if missing(first_rep)
replace first_dem=0 if missing(first_dem)

gen independent3 = ln(second_rep+1) - ln(first_rep+1) - ln(second_dem+1) ///
 + ln(first_dem +1) 
 
saveold final_merge_all.dta, replace

*creating a list of disticts for which I have data.
tab ID2 if missing( can_inc_cha_ope_sea1) |missing(can_inc_cha_ope_sea2)
*This list will be excluded from all future regressions, since at least 1 candidate does not have expenditure data

 *****************************************JUST ADS AND EVENTS*************************************
 */
use spending_all_states.dta, clear 
keep  if code ==4|code==7
 
collapse (sum)  dis_amo, by(can_id can_off can_off_sta can_off_dis day)

gen cum_spend = .

compress

foreach day of numlist 0/150 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend0- cum_spend150 {
replace cum_spend = `var' if cum_spend==.
}
drop cum_spend0- cum_spend150

foreach day of numlist 151/300 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend151- cum_spend300 {
replace cum_spend = `var' if cum_spend==.
}

drop cum_spend151- cum_spend300

foreach day of numlist 301/367 {
egen cum_spend`day' = sum(dis_amo) if day <=`day', by(can_id)
}

foreach var of varlist  cum_spend301- cum_spend367 {
replace cum_spend = `var' if cum_spend==.
}

drop cum_spend301- cum_spend367

*i need to fill in the missing day with the cumulative spending of the previous day
drop if missing(day)
drop dis_amo

reshape wide cum_spend, i(can_id can_off can_off_sta can_off_dis) j(day)

reshape long cum_spend, i(can_id can_off can_off_sta can_off_dis) j(day)

sort can_id day
/*replacing the missing value (on a day in which 0 was spent) with the closest 
previous cumulative spending value*/
by can_id: replace cum_spend = cum_spend[_n-1] if cum_spend==.

*detemining the beginning of the general election by the end of the primary
replace day = day+365
gen begin_day = .
replace begin_day = 1 if day==481 & can_off_sta=="AL"
replace begin_day = 1 if day==638 & can_off_sta=="AK"
replace begin_day = 1 if day==638 & can_off_sta=="AZ"
replace begin_day = 1 if day == 530 & can_off_sta=="AR"
replace begin_day = 1 if day == 523 & can_off_sta=="CA"
replace begin_day = 1 if day == 544 & can_off_sta=="CO"
replace begin_day = 1 if day == 603 & can_off_sta=="CT"
replace begin_day = 1 if day == 621 & can_off_sta=="DE"
replace begin_day = 1 if day == 595 & can_off_sta=="FL"
replace begin_day = 1 if day == 600 & can_off_sta=="GA"
replace begin_day = 1 if day == 590 & can_off_sta=="HI"
replace begin_day = 1 if day == 502 & can_off_sta=="ID"
replace begin_day = 1 if day == 446 & can_off_sta=="IL"
replace begin_day = 1 if day == 495 & can_off_sta=="IN"
replace begin_day = 1 if day == 523 & can_off_sta=="IA"
replace begin_day = 1 if day == 586 & can_off_sta=="KS"
replace begin_day = 1 if day == 530 & can_off_sta=="KY"
replace begin_day = 1 if day == 360 & can_off_sta=="LA"
replace begin_day = 1 if day == 530 & can_off_sta=="ME"
replace begin_day = 1 if day == 460 & can_off_sta=="MD"
replace begin_day = 1 if day == 616 & can_off_sta=="MA"
replace begin_day = 1 if day == 587 & can_off_sta=="MI"
replace begin_day = 1 if day == 593 & can_off_sta=="MN"
replace begin_day = 1 if day == 460 & can_off_sta=="MS"
replace begin_day = 1 if day == 586 & can_off_sta=="MO"
replace begin_day = 1 if day == 523 & can_off_sta=="MT"
replace begin_day = 1 if day == 502 & can_off_sta=="NE"
replace begin_day = 1 if day == 530 & can_off_sta=="NV"
replace begin_day = 1 if day == 621 & can_off_sta=="NH"
replace begin_day = 1 if day == 523 & can_off_sta=="NJ"
replace begin_day = 1 if day == 523 & can_off_sta=="NM"
replace begin_day = 1 if day == 544 & can_off_sta=="NY"
replace begin_day = 1 if day == 565 & can_off_sta=="NC"
replace begin_day = 1 if day == 530 & can_off_sta=="ND"
replace begin_day = 1 if day == 432 & can_off_sta=="OH"
replace begin_day = 1 if day == 607 & can_off_sta=="OK"
replace begin_day = 1 if day == 502 & can_off_sta=="OR"
replace begin_day = 1 if day == 481 & can_off_sta=="PA"
replace begin_day = 1 if day == 621 & can_off_sta=="RI"
replace begin_day = 1 if day == 544 & can_off_sta=="SC"
replace begin_day = 1 if day == 544 & can_off_sta=="SD"
replace begin_day = 1 if day == 591 & can_off_sta=="TN"
replace begin_day = 1 if day == 579 & can_off_sta=="TX"
replace begin_day = 1 if day == 544 & can_off_sta=="UT"
replace begin_day = 1 if day == 606 & can_off_sta=="VT"
replace begin_day = 1 if day == 530 & can_off_sta=="VA"
replace begin_day = 1 if day == 586 & can_off_sta=="WA"
replace begin_day = 1 if day == 495 & can_off_sta=="WV"
replace begin_day = 1 if day == 593 & can_off_sta=="WI"
replace begin_day = 1 if day == 600 & can_off_sta=="WY"

replace begin_day = 2 if day==676
replace begin_day = 3 if day==732

rename can_off_dis can_off_dis_code
*master FEC candidate data list taken from http://www.fec.gov/finance/disclosure/ftpdet.shtml#a2011_2012
merge m:1 can_id using cand_master_list.dta
keep if _merge ==1 | _merge==3
drop _merge

*merge with dataset that will identify whether the politician was a candidate in the general election
merge m:1 can_id using cand_list2.dta
*Some candidates are missing. Fixed below
replace candidate = 0 if can_id=="H2CA21159"
replace candidate = 0 if can_id=="H2MI01118"
replace candidate = 0 if can_id=="H2NV02239"
replace candidate = 0 if can_id=="H4IN02051"
replace candidate = 0 if can_id=="H6OH23033"

drop if missing( can_off_sta)

keep if candidate==1

*fixing mis-assigned candidates
replace can_party = "REP" if can_id=="H2NY05084"
replace can_party = "REP" if can_id=="H0GA07133"
replace can_party = "DEM" if can_id=="H2NY22113"
replace can_party = "REP" if can_id=="H2NY26171"
replace can_party = "REP" if can_id=="H2PA14084"
replace can_inc_cha_ope_sea="CHALLENGER" if can_id=="H2NY05084"
*generate party code (1=dem, 2=rep)
gen party = 1 if can_party=="DEM" | can_party=="DFL"
replace party = 2 if can_party=="REP"
keep if party== 1|party==2
drop if can_id=="S2ME00059"
drop if can_id=="H8CA36097"
drop if can_id=="H8CA37137"
drop if can_id=="H2NY13088"
drop if can_id=="H0NY04057"
drop if can_id == "H2LA01075"
replace party=2 if can_id =="H2NY05084"
*Florida district 4 has IND, no DEM
replace can_party = "IND" if can_id=="H2FL04112"
replace  party=3 if  can_party=="IND"
*districts in which 2 people from the same party are competing
gen can_off_dis2=real( can_off_dis)
drop  can_off_dis_code
rename  can_off_dis2  can_off_dis
drop if can_off_sta=="LA" & can_off_dis==3
drop if can_off_sta=="LA" & can_off_dis==2
drop if can_off_sta=="CA" & can_off_dis==43
drop if can_off_sta=="CA" & can_off_dis==39
drop if can_off_sta=="CA" & can_off_dis==35
drop if can_off_sta=="CA" & can_off_dis==31
drop if can_off_sta=="CA" & can_off_dis==30
drop if can_off_sta=="CA" & can_off_dis==15
drop if can_off_sta=="CA" & can_off_dis==8

drop _merge

save spend_detail_allspend_ads.dta, replace

*********************SPENDING PATTERNS, JUST ADS & EVENTS*************************
*creating aggregate spending by incumbent/challenger/open category
*dropping uncontested elections. The value of sum below will be equal to 368 if there is only 1 candidate
keep if var2=="W"

gen uncontested= can_name=="ROBY, MARTHA"|can_name=="ROSS, DENNIS ALAN"|can_name=="WILSON, FREDERICA S" ///
|can_name=="WESTMORELAND, LYNN A."|can_name=="SCOTT, JAMES AUSTIN"|can_name=="BROUN, PAUL COLLINS"| ///
can_name=="HUELSKAMP, TIMOTHY A REPRESENTA"|can_name=="NEAL, RICHARD E MR."|can_name=="MCGOVERN, JIM"| ///
can_name=="SERRANO, JOSE E"|can_name=="BOEHNER, JOHN A"|can_name=="FUDGE, MARCIA L"|can_name=="WILSON, ADDISON (JOE) GRAVES"| ///
can_name=="JOHNSON, SAMUEL ROBERT"

collapse (mean) cum_spend, by( uncontested day)

reshape wide   cum_spend, i( day) j( unc)

label var  cum_spend1 "Uncontested"
label var  cum_spend0 "Contested"

replace  cum_spend0= cum_spend0/1000
replace  cum_spend1= cum_spend1/1000
replace  day =  day-365

line cum_spend0  cum_spend1 day, ///
  xline(80 245 310, lcolor(black) lpattern(dash)) ///
  ytitle("Cumulative Spending (in thousands)") ///
  xlabel(0 "Year Begin" 60 "March 1"  245 "Sept. 1" 310 "Election Day" ) ///
  legend(pos(9) ring(0) col(1)) ///
  xtitle("Day of year (2012)") 
  graph save graph2b, replace
 
 /*i need to merge this into the ANES data twice. once for the 
day of the survey, and another for the pre, post, and final 
election spending*/

use spend_detail_allspend_ads.dta, clear

*this one if for the day of the preelection survey
drop can_id var2 can_party candidate can_name
reshape wide  cum_spend  can_inc_cha_ope_sea, i(can_off can_off_sta can_off_dis ///
day begin_day) j(party)

*creating a unique identifier that will provides the day and district
egen ID = concat(can_off can_off_sta can_off_dis day), punc(-)

keep cum_spend1 can_inc_cha_ope_sea1 cum_spend2 can_inc_cha_ope_sea2 cum_spend3 can_inc_cha_ope_sea3 ID

replace cum_spend1 = 0 if (can_inc_cha_ope_sea1=="OPEN"|can_inc_cha_ope_sea1=="INCUMBENT"| ///
can_inc_cha_ope_sea1=="CHALLENGER") & missing(cum_spend1)

replace cum_spend2 = 0 if (can_inc_cha_ope_sea2=="OPEN"|can_inc_cha_ope_sea2=="INCUMBENT"| ///
can_inc_cha_ope_sea2=="CHALLENGER") & missing(cum_spend2)

gen day = substr( ID, 8,10)
gen day1= real(day)
replace day1 = abs(day1)
keep if day1>610 & day1<690
drop day day1
save day_of_ads.dta, replace

*this is for election-day and final spending
use spend_detail_allspend_ads.dta, clear

keep if  begin_day!=.
drop day
replace cum_spend=0 if missing(cum_spend)
merge m:1 can_id using transfer_collapsed2.dta
keep if _merge==1|_merge==3
drop _merge
reshape wide  cum_spend, i( can_id can_off can_off_sta can_off_dis ///
can_party can_inc_cha_ope_sea candidate party amount) j( begin_day)

rename cum_spend1 cum_spend1_
rename cum_spend2 cum_spend2_
rename cum_spend3 cum_spend3_

drop can_id can_party  candidate var2 can_name

reshape wide cum_spend1_ cum_spend2_ cum_spend3_  can_inc_cha_ope_sea amount, ///
i(can_off can_off_sta can_off_dis ) j(party)


egen ID2 = concat(can_off  can_off_sta can_off_dis), punc(-)

save spend_reference_ads.dta, replace

*taking the anes data created above
use anes_house.dta, clear

*dropping those who already voted
drop if prevote_voted==1

egen ID = concat(house sample_state sample_district day), punc(-)

merge m:1 ID using day_of_ads.dta

keep if _merge==3|_merge==1
drop _merge

egen ID2 = concat(house sample_state sample_district), punc(-)
merge m:1 ID2 using spend_reference_ads.dta

save final_merge_ads.dta, replace

label var  cum_spend1 "Dem Spend Today"
label var  cum_spend2 "Rep Spend Today"
label var  cum_spend1_1 "Dem Spend Beginning"
label var  cum_spend2_1 "Dem Spend Election"
label var  cum_spend3_1 "Dem Spend Final"

label var  cum_spend1_2 "Rep Spend Beginning"
label var  cum_spend2_2 "Rep Spend Election"
label var  cum_spend3_2 "Rep Spend Final"

gen first_rep = cum_spend2 - cum_spend1_2
replace first_rep = 0 if first_rep<0
replace first_rep = 0 if missing(first_rep)
gen second_rep = cum_spend2_2 - cum_spend1_2
replace second_rep = 0 if second_rep<0

gen first_dem = cum_spend1 - cum_spend1_1
replace first_dem = 0 if first_dem<0
replace first_dem = 0 if missing(first_dem)
gen second_dem = cum_spend2_1 - cum_spend1_1
replace second_dem = 0 if second_dem<0

gen pre_vote = 1 if  prevote_intent_bc==2
replace pre_vote = 1 if prevote_intent_nobc==82
replace pre_vote = -1 if prevote_intent_nobc==81
replace pre_vote = -1 if prevote_intent_bc==1
replace pre_vote = 0 if prevote_intent_bc==-8| prevote_intent_bc>2
replace pre_vote = 0 if prevote_intent==2
replace pre_vote = 0 if prevote_intent_nobc==-8|prevote_intent_nobc==87

gen post_vote = 1 if postvote_bc == 2
replace post_vote = -1 if postvote_bc == 1
*people who are coded as -1 are listed here as having not voted (post_vote==0)
replace post_vote = 0 if postvote_bc <0
replace post_vote = 0 if postvote==2
replace post_vote = -1 if postvote_nobc==81 & missing(post_vote)
replace post_vote = 1 if postvote_nobc==82 & missing(post_vote)
replace post_vote = 0 if postvote_nobc==87 & missing(post_vote)

gen dependent = post_vote-pre_vote

gen independent = second_rep - first_rep - second_dem + first_dem
gen independent2 = ln(second_rep+1) - ln(first_rep+1) - ln(second_dem+1) ///
 + ln(first_dem +1)

save final_merge_ads.dta, replace

*creating male variable
gen male = 1 if gender==1
replace male = 0 if missing(male)
*generating married variable
gen married = 1 if dem_marital==1
replace married = 0 if missing(married)
*generating age variable
gen age  = 18 if  dem_agegrp_iwdate ==1
replace age  = 22 if  dem_agegrp_iwdate ==2
replace age  = 27 if  dem_agegrp_iwdate ==3
replace age  = 32 if  dem_agegrp_iwdate ==4
replace age  = 37 if  dem_agegrp_iwdate ==5
replace age  = 42 if  dem_agegrp_iwdate ==6
replace age  = 47 if  dem_agegrp_iwdate ==7
replace age  = 52 if  dem_agegrp_iwdate ==8
replace age  = 57 if  dem_agegrp_iwdate ==9
replace age  = 62 if  dem_agegrp_iwdate ==10
replace age  = 67 if  dem_agegrp_iwdate ==11
replace age  = 72 if  dem_agegrp_iwdate ==12
replace age  = 80 if  dem_agegrp_iwdate ==13
*generating foreign_born variable
*gen for_born = dem_nativity>3
*replace for_born = . if dem_nativity<1

label var male "Male"
label var married "Married"
label var age "Age"

replace amount1=0 if missing(amount1)
replace amount2=0 if missing(amount2)

*the following groups are eliminated from regressions
*wave_completions!=1. This eliminates anyone who didn't participate in both rounds of survey
*(independent2<10 & independent2>-10). Removes outliers (less than 20 obs)

*Summary of voter pre and post election survey (Table 1)
tab pre_vote post_vote if !missing(dependent) & !missing( independent2) & !missing( amount1 ) & !missing(amount2)   & (independent2<10 & independent2>-10) & wave_c==1

sum first_dem second_dem first_rep second_rep male married age  pre_vote post_vote if !missing(dependent) & !missing( independent2) & !missing( amount1 ) & !missing(amount2)   & (independent2<10 & independent2>-10) & wave_c==1
*regressions
*creating cross-section spending variable
gen cross_ind = ln(second_rep +1) -ln(second_dem +1)
label var cross_ind "Log(Spending)"
save final_merge_ads.dta, replace

reg  post_vote cross_ind  married male age  if  post_admin_lang_start!=-6   & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent2 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
areg dependent  married male age  independent2 i.day  if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta) absorb(ID2)
ologit dependent independent2 i.day amount1 amount2 if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)

*effect of 100,000 injection
gen independent3 = ln(second_rep+100000) - ln(first_rep+1) - ln(second_dem+1) + ln(first_dem +1)
sum independent3 independent2 if e(sample)
* difference is 1.734-0.007=1.728

*different margins
*party switchers
reg dependent independent2 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & pre_vote!=0 & post_vote!=0 & wave_==1, cluster( can_off_sta)
*turnout margin (people either didnt intend to vote for either, or didn't vote for either)
reg dependent independent2 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & (pre_vote==0| post_vote==0) & wave_==1, cluster( can_off_sta)
*people who didn't vote for either
reg dependent independent2 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & (post_vote==0) & wave_==1, cluster( can_off_sta)

***INDEPENDENTS
gen indies =  pid_self==3
gen inter2 = indies* independent2
reg dependent independent2 i.day amount1 amount2 indies inter2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10)& wave==1, cluster( can_off_sta)
areg dependent independent2 i.day  amount1 amount2 indies inter2 married male age   if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave==1, cluster( can_off_sta) absorb(ID2)
ologit dependent independent2 i.day amount1 amount2 indies inter2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10)& wave==1, cluster( can_off_sta)

***ECONOMIC VOTING
gen bad =   econ_ecnow>3
gen inter3 = bad* independent2
reg dependent independent2 i.day bad inter3 amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10)& wave==1, cluster( can_off_sta)
areg dependent independent2 i.day  bad inter3 amount1 amount2 married male age  if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave==1, cluster( can_off_sta) absorb(ID2)
ologit dependent independent2 i.day bad inter3 amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10)& wave==1, cluster( can_off_sta)
*effect of 100,000 injection if district was only "bad" was found by flipping the interaction term

***KNOWLEDGE
gen high = knowl_housemaj==2
replace high = high +1 if knowl_senmaj==1
replace high= high+1 if cses_poliinfthree==1
replace high = high+1 if cses_poliinfone==4
replace high = high+1 if cses_polinftwo==2
replace high = high+1 if cses_poliinffour==3
replace high=high/6
gen inter4 = high* independent2
reg dependent independent2 i.day high inter4 amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10)& wave==1, cluster( can_off_sta)
areg dependent independent2 i.day  high inter4 amount1 amount2 married male age  if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave==1, cluster( can_off_sta) absorb(ID2)
ologit dependent independent2 i.day high inter4 amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10)& wave==1, cluster( can_off_sta)


gen winner = 0 if (ID2=="H-AL-7"|ID2=="H-AZ-1"|ID2=="H-AZ-2"|ID2=="H-AZ-3"|ID2=="H-AZ-7"|ID2=="H-AZ-9" ///
|ID2=="H-CA-2"|ID2=="H-CA-3"|ID2=="H-CA-5"|ID2=="H-CA-6"|ID2=="H-CA-7"|ID2=="H-CA-9"|ID2=="H-CA-11"|ID2=="H-CA-12"|ID2=="H-CA-13"|ID2=="H-CA-14"|ID2=="H-CA-15"|ID2=="H-CA-16"|ID2=="H-CA-17"|ID2=="H-CA-18"|ID2=="H-CA-19"|ID2=="H-CA-20" ///
|ID2=="H-CA-24"|ID2=="H-CA-26"|ID2=="H-CA-27"|ID2=="H-CA-28"|ID2=="H-CA-29"|ID2=="H-CA-30"|ID2=="H-CA-32"|ID2=="H-CA-33"|ID2=="H-CA-34"|ID2=="H-CA-35"|ID2=="H-CA-36"|ID2=="H-CA-37"|ID2=="H-CA-38"|ID2=="H-CA-40"|ID2=="H-CA-41" ///
|ID2=="H-CA-43"|ID2=="H-CA-44"|ID2=="H-CA-46"|ID2=="H-CA-47"|ID2=="H-CA-51"|ID2=="H-CA-52"|ID2=="H-CA-53"|ID2=="H-CO-1"|ID2=="H-CO-2"|ID2=="H-CO-7"|can_off_sta=="CT"|can_off_sta=="DE" |ID2=="H-FL-5"|ID2=="H-FL-9"|ID2=="H-FL-14"|ID2=="H-FL-18")
replace winner=0 if (ID2=="H-FL-20"|ID2=="H-FL-21"|ID2=="H-FL-22"|ID2=="H-FL-23"|ID2=="H-FL-24"|ID2=="H-FL-26"|ID2=="H-GA-2"|ID2=="H-GA-4"|ID2=="H-GA-5"|ID2=="H-GA-12"|ID2=="H-GA-13"|can_off_sta=="HI" ///
|ID2=="H-IL-1"|ID2=="H-IL-2"|ID2=="H-IL-3"|ID2=="H-IL-4"|ID2=="H-IL-5"|ID2=="H-IL-7"|ID2=="H-IL-8"|ID2=="H-IL-9"|ID2=="H-IL-10"|ID2=="H-IL-11"|ID2=="H-IL-12"|ID2=="H-IL-17"|ID2=="H-IN-1"|ID2=="H-IN-7"|ID2=="H-IA-1"|ID2=="H-IA-2"|ID2=="H-KY-3" ///
|ID2=="H-LA-2"|can_off_sta=="ME"|ID2=="H-MD-2"|ID2=="H-MD-3"|ID2=="H-MD-4"|ID2=="H-MD-5"|ID2=="H-MD-6"|ID2=="H-MD-7"|ID2=="H-MD-8"|can_off_sta=="MA"|ID2=="H-MI-5"|ID2=="H-MI-9"|ID2=="H-MI-12"|ID2=="H-MI-13"|ID2=="H-MI-14" ///
|ID2=="H-MN-1"|ID2=="H-MN-4"|ID2=="H-MN-5"|ID2=="H-MN-7"|ID2=="H-MN-8"|ID2=="H-MS-2"|ID2=="H-MO-1"|ID2=="H-MO-5"|ID2=="H-NV-1"|ID2=="H-NV-4")
replace winner=0 if (can_off_sta=="NH"|ID2=="H-NJ-1"|ID2=="H-NJ-6"|ID2=="H-NJ-8"|ID2=="H-NJ-9"|ID2=="H-NJ-10"|ID2=="H-NJ-12"|ID2=="H-NM-1"|ID2=="H-NM-3"|ID2=="H-NY-1"|ID2=="H-NY-3"|ID2=="H-NY-4"|ID2=="H-NY-5"|ID2=="H-NY-6"|ID2=="H-NY-7"|ID2=="H-NY-8"|ID2=="H-NY-9"|ID2=="H-NY-10" ///
|ID2=="H-NY-12"|ID2=="H-NY-13"|ID2=="H-NY-14"|ID2=="H-NY-15"|ID2=="H-NY-16"|ID2=="H-NY-17"|ID2=="H-NY-18"|ID2=="H-NY-20"|ID2=="H-NY-21"|ID2=="H-NY-25"|ID2=="H-NY-24"|ID2=="H-NY-26"|ID2=="H-NC-1"|ID2=="H-NC-4"|ID2=="H-NC-12" ///
|ID2=="H-OH-3"|ID2=="H-OH-9"|ID2=="H-OH-11"|ID2=="H-OH-13"|ID2=="H-OR-1"|ID2=="H-OR-3"|ID2=="H-OR-4"|ID2=="H-OR-5"|ID2=="H-PA-1"|ID2=="H-PA-2"|ID2=="H-PA-13"|ID2=="H-PA-14"|ID2=="H-PA-17" |can_off_sta=="RI"|ID2=="H-SC-6" ///
|ID2=="H-TN-5"|ID2=="H-TN-9"|ID2=="H-TX-9"|ID2=="H-TX-15"|ID2=="H-TX-16"|ID2=="H-TX-18"|ID2=="H-TX-20"|ID2=="H-TX-9"|ID2=="H-TX-28"|ID2=="H-TX-29"|ID2=="H-TX-30"|ID2=="H-TX-33"|ID2=="H-TX-34"|ID2=="H-TX-35")
replace winner=0 if (ID2=="H-UT-4"|can_off_sta=="VT"|ID2=="H-VA-3"|ID2=="H-VA-8"|ID2=="H-VA-11"|ID2=="H-WA-1"|ID2=="H-WA-2"|ID2=="H-WA-3"|ID2=="H-WA-6"|ID2=="H-WA-7"|ID2=="H-WA-9"|ID2=="H-WA-10"|ID2=="H-WV-3" ///
|ID2=="H-W2-2"|ID2=="H-WI-3"|ID2=="H-WI-4")
replace winner = 1 if missing(winner)

reg dependent winner married male age  i.day if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)


forvalues i=0(1)12   {
 /*i need to merge this into the ANES data twice. once for the 
day of the survey, and another for the pre, post, and final 
election spending*/

use code`i'.dta, clear

*this one if for the day of the preelection survey
drop can_id var2 can_party candidate can_name
reshape wide  cum_spend  can_inc_cha_ope_sea, i(can_off can_off_sta can_off_dis ///
day begin_day) j(party)

*creating a unique identifier that will provides the day and district
egen ID = concat(can_off can_off_sta can_off_dis day), punc(-)

keep cum_spend1 can_inc_cha_ope_sea1 cum_spend2 can_inc_cha_ope_sea2 ID

replace cum_spend1 = 0 if (can_inc_cha_ope_sea1=="OPEN"|can_inc_cha_ope_sea1=="INCUMBENT"| ///
can_inc_cha_ope_sea1=="CHALLENGER") & missing(cum_spend1)

replace cum_spend2 = 0 if (can_inc_cha_ope_sea2=="OPEN"|can_inc_cha_ope_sea2=="INCUMBENT"| ///
can_inc_cha_ope_sea2=="CHALLENGER") & missing(cum_spend2)

gen day = substr( ID, 8,10)
gen day1= real(day)
replace day1 = abs(day1)
keep if day1>610 & day1<690
drop day day1

rename  cum_spend1 cum_spend1_code`i'
rename  cum_spend2 cum_spend2_code`i'

drop can_inc_cha_ope_sea1 can_inc_cha_ope_sea2 
save day_of_`i'.dta, replace

*this is for election-day and final spending
use code`i'.dta, clear

keep if  begin_day!=.
drop day
replace cum_spend=0 if missing(cum_spend)
reshape wide  cum_spend, i( can_id can_off can_off_sta can_off_dis ///
can_party can_inc_cha_ope_sea candidate party) j( begin_day)

rename cum_spend1 cum_spend1_
rename cum_spend2 cum_spend2_
 
keep can_off can_off_sta can_inc_cha_ope_sea party can_off_dis cum_spend1_ cum_spend2_

reshape wide cum_spend1_ cum_spend2_  can_inc_cha_ope_sea, ///
i(can_off can_off_sta can_off_dis ) j(party)

egen ID2 = concat(can_off  can_off_sta can_off_dis), punc(-)
rename  cum_spend1_1 cum_spend1_1_code`i'
rename  cum_spend2_1 cum_spend2_1_code`i'
rename  cum_spend1_2 cum_spend1_2_code`i'
rename  cum_spend2_2 cum_spend2_2_code`i'

drop can_inc_cha_ope_sea1 can_inc_cha_ope_sea2 
save spend_reference_`i'.dta, replace
}


use "C:\Users\ssprickschuster\Dropbox\Candidate Spending\Paper3\Replication\final_merge_ads.dta", clear
gen indies =  pid_self==3
gen moderate =   libcpre_self==4
gen bad =   econ_ecnow>3
drop _merge
compress
drop interest_attention- discrim_women

forvalues i=0(1)12   {
merge m:1 ID using day_of_`i'.dta
drop if _merge==2
drop _merge
merge m:1 ID2 using spend_reference_`i'.dta
drop _merge
gen first_rep`i' = cum_spend2_code`i' - cum_spend1_2_code`i'
replace first_rep`i' = 0 if first_rep`i'<0
replace first_rep`i' = 0 if missing(first_rep`i')
gen second_rep`i' = cum_spend2_2_code`i' - cum_spend1_2_code`i'
replace second_rep`i' = 0 if second_rep`i'<0
replace second_rep`i' = 0 if missing(second_rep`i')


gen first_dem`i' = cum_spend1_code`i' - cum_spend1_1_code`i'
replace first_dem`i' = 0 if first_dem`i'<0
replace first_dem`i' = 0 if missing(first_dem`i')
gen second_dem`i' = cum_spend2_1_code`i' - cum_spend1_1_code`i'
replace second_dem`i' = 0 if second_dem`i'<0
replace second_dem`i' = 0 if missing(second_dem`i')
gen independent_code`i' = ln(second_rep`i'+1) - ln(first_rep`i'+1) - ln(second_dem`i'+1)  + ln(first_dem`i' +1)
replace independent_code`i' = 0 if missing(independent_code`i')
 }

 
reg dependent independent_code1 i.day amount1 amount2 if  (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code2 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code3 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code5 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code6 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code8 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code9 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code10 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code11 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
reg dependent independent_code12 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)

gen independent_all  = ln( second_rep0+ second_rep1+ second_rep2+ second_rep3+second_rep4+ second_rep5+ second_rep6+ second_rep7+ second_rep8+ second_rep9+ second_rep10+ second_rep11+ second_rep12+1) ///
 - ln( first_rep0+first_rep1+ first_rep2+ first_rep3+first_rep4+ first_rep5+ first_rep6+ first_rep7+ first_rep8+first_rep9+first_rep10+first_rep11+first_rep12 ) ///
 -ln( second_dem0+second_dem1+ second_dem2+ second_dem3+second_dem4+ second_dem5+ second_dem6+ second_dem7+second_dem8+second_dem9+second_dem10+second_dem11+second_dem12+1) ///
 + ln( first_dem0+first_dem1+ first_dem2+ first_dem3+first_dem4+ first_dem5+ first_dem6+ first_dem7+ first_dem8+ first_dem9+ first_dem10+ first_dem11+ first_dem12+1)  
replace independent_all = 0 if missing(independent_all)

gen cross_ind_all = ln( second_rep0+ second_rep1+ second_rep2+ second_rep3+second_rep4+ second_rep5+ second_rep6+ second_rep7+ second_rep8+ second_rep9+ second_rep10+ second_rep11+ second_rep12+1) ///
 -ln( second_dem0+second_dem1+ second_dem2+ second_dem3+second_dem4+ second_dem5+ second_dem6+ second_dem7+second_dem8+second_dem9+second_dem10+second_dem11+second_dem12+1) 


qui reg  post_vote cross_ind_all  married male age  if  post_admin_lang_start!=-6   & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(cross_ind_all) b se t
reg dependent independent_all i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_all) b se t
areg dependent  married male age  independent_all i.day  if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta) absorb(ID2)
est table, keep(independent_all) b se t
qui ologit dependent independent_all i.day amount1 amount2 if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_all) b se t

gen independent_w1 = ln(second_rep + second_rep1+1) - ln(first_rep + first_rep1+1) - ln(second_dem+second_dem1+1) ///
 + ln(first_dem +first_dem1+1)
gen independent_w1_2 = ln(second_rep +  second_rep2+  second_rep1+1) - ln(first_rep +first_rep2+first_rep1+1) - ln(second_dem+second_dem2+second_dem1+1) ///
 + ln(first_dem +first_dem2+first_dem1+1)
 gen independent_w1_2_6 = ln(second_rep + second_rep1+ second_rep2+ second_rep6+1) - ln(first_rep + first_rep1+ first_rep2+ first_rep6+1) - ln(second_dem+second_dem1+second_dem2+second_dem6+1) ///
 + ln(first_dem +first_dem1+first_dem2+first_dem6+1)
 gen independent_w3 = ln(second_rep + second_rep3+1) - ln(first_rep + first_rep3+1) - ln(second_dem+second_dem3+1) ///
 + ln(first_dem +first_dem3+1)
 
qui reg dependent independent_w1 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_w) b se t
qui areg dependent  married male age  independent_w1 i.day  if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta) absorb(ID2)
est table, keep(independent_w) b se t
qui ologit dependent independent_w1 i.day amount1 amount2 if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_w) b se t

 qui reg dependent independent_w1_2 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_w) b se t
qui areg dependent  married male age  independent_w1_2 i.day  if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta) absorb(ID2)
est table, keep(independent_w) b se t
qui ologit dependent independent_w1_2 i.day amount1 amount2 if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_w) b se t

qui reg dependent independent_w1_2_6 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_w) b se
qui areg dependent  married male age  independent_w1_2_6 i.day  if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta) absorb(ID2)
est table, keep(independent_w) b se
qui ologit dependent independent_w1_2_6 i.day amount1 amount2 if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_w) b se

 reg dependent independent_w3 i.day amount1 amount2 if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_w) b se
qui areg dependent  married male age  independent_w3 i.day  if  post_admin_lang_start!=-6  & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta) absorb(ID2)
est table, keep(independent_w) b se
qui ologit dependent independent_w3 i.day amount1 amount2 if  post_admin_lang_start!=-6 & (independent2<10 & independent2>-10) & wave_==1, cluster( can_off_sta)
est table, keep(independent_w) b se
